How to change Colored to Black and white(GrayScale) when touch in android

帅比萌擦擦* 提交于 2019-12-25 04:23:06

问题


I have Horizontal layout with dynamically add imageview using adding image on linear layout. and all horizontal view add scorllview vertically

||||||| when touch then change colored to grayscred this view(view added images here)
|||||||
|||||||
|||||||

like this,

now if i on touch horizontal scoll view , i want to change defualt image is colored to grayscaled(black and white). during ontouch on horizontal scorllview putting but could not reconized. and not changing effect. what i do.

My Main Intention is: When Horizontall Scrolling Image, Only window view(touched view) grayscred ohters remains previous stage(colored).

Here is the my code works on image but i want to on horizontal view.

public class FragmentOne extends Fragment  {

    ImageView ivIcon;
    TextView tvItemName;

    public static final String IMAGE_RESOURCE_ID = "iconResourceID";
    public static final String ITEM_NAME = "itemName";
    public HorizontalScrollView horizontalScrollView;
    public ScrollView scollView;
    RelativeLayout  relativeSubMenu;
    RelativeLayout relativeLayoutMain;
    public Context mcontext;
    private LinearLayout linearLayout;

    public FragmentOne() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_layout_one, container,
                false);
        mcontext = Common.getContext();
        relativeLayoutMain = (RelativeLayout) view.findViewById(R.id.rl);
        // scollView=(ScrollView) view.findViewById(R.id.verticalScroll);


        // bottom linear layout for yello ads

        LinearLayout linearBottom = new LinearLayout(mcontext);
        linearBottom.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
        linearBottom.setBackgroundColor(Color.YELLOW);
        linearBottom.setGravity(Gravity.BOTTOM);
        linearBottom.setOrientation(LinearLayout.HORIZONTAL);

        for (int i = 0; i < 5; i++) {
            TextView tv= new TextView(mcontext);
            tv.setTag(i);
            tv.setText("Yelllo Ad "+i);
            linearBottom.addView(tv);
        }
        //end 

        ScrollView scrollView = new ScrollView(mcontext);
        scrollView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        LinearLayout linearLayourScrollView = new LinearLayout(mcontext);
        linearLayourScrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
        linearLayourScrollView.setOrientation(1);

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);

            params.gravity = Gravity.BOTTOM;
        for (int s = 0; s < 7; s++) {
            final HorizontalScrollView horizontalScrollView = new HorizontalScrollView(mcontext);
            relativeSubMenu = new RelativeLayout(mcontext);
            horizontalScrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT));
             linearLayout = new LinearLayout(mcontext);
            linearLayout.setTag(s);
            //linearLayout.setOntouchListener(this)
            linearLayout.setFocusableInTouchMode(true);
            //linearLayout.setBackgroundResource(R.drawable.backgroundcolor);
            linearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            linearLayout.setOrientation(0);
            horizontalScrollView.setTag(s);
            horizontalScrollView.setTag(s);
            for (int h = 0; h < 10; h++) {
                final ImageView imageView = new ImageView(mcontext);
                imageView.setTag(h);

                imageView.setImageResource(R.drawable.images);
                /*Resources r = getResources();
                Drawable[] layers = new Drawable[2];
                layers[0] = r.getDrawable(R.drawable.t);
                layers[1] = r.getDrawable(R.drawable.tt);
                LayerDrawable layerDrawable = new LayerDrawable(layers);
                testimage.setImageDrawable(layerDrawable);*/
                imageView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Toast.makeText(mcontext, "Category="+horizontalScrollView.getTag()+" ad id="+imageView.getTag(), Toast.LENGTH_SHORT).show();
                        showShortDetails( horizontalScrollView.getTag(),imageView.getTag());
                    }
                });

                imageView.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {

                        switch (event.getAction()) {
                            case MotionEvent.ACTION_DOWN: {
                                ImageView view = (ImageView) v;
                                //overlay is black with transparency of 0x77 (119)
                               // view.getDrawable().setColorFilter(0x77000000,PorterDuff.Mode.DST_ATOP);

                                //ImageView Sun = (ImageView)findViewById(R.id.sun);
                                view.getDrawable().setColorFilter(ColorFilterGenerator.adjustHue(333));
                                 view.invalidate();
                                //linearLayout.setBackgroundColor(Color.RED);
                                Log.i("Relative", "On Touch");
                                //hsv.invalidate();
                                break;
                            }
                            case MotionEvent.ACTION_UP:
                            case MotionEvent.ACTION_CANCEL: {
                                ImageView view = (ImageView) v;
                                //clear the overlay
                                view.getDrawable().clearColorFilter();
                                view.invalidate();
                                //relativeSubMenu.setBackgroundColor(Color.parseColor("#00FFFF00"));
                                //linearLayout.setBackgroundColor(Color.WHITE);
                                Log.i("Relative", "No Touch");
                                break;
                            }
                        }

                        return false;
                    }
                });
                linearLayout.addView(imageView);
            }
            horizontalScrollView.addView(linearLayout);
            relativeSubMenu.addView(horizontalScrollView);
            linearLayourScrollView.addView(relativeSubMenu);
        }
        scrollView.addView(linearLayourScrollView);
        relativeLayoutMain.addView(scrollView);
        relativeLayoutMain.addView(linearBottom, params);
        return view;
    }

回答1:


You can add background for layout in your xml file and set a custom layout for your background like this

android:background="@drawable/custombackground"

custombackground.xml

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_window_focused="false"
        android:state_enabled="true"
        android:color="your color code" />

    <item
        android:state_window_focused="false"
        android:state_enabled="false"
        android:color="your color code" />

    <item
        android:state_pressed="true"
        android:color="your color code" />

    <item
        android:state_enabled="true"
        android:state_focused="true"
        android:color = "#00000000"/>

    <item
        android:state_enabled="true"
        android:color="your color code" />

    <item
        android:state_focused="true"
        android:color="your color code" />

    <item
        android:color="your color code" />

</selector>

hope it will help you :)




回答2:


Basically,your requirement is to change the color of imageview when you touch it,for that you have to implement the color changing code in case MotionEvent.ACTION_DOWN: for example the changing color is light gray so the code will be

v.setBackgroundColor(Color.LTGRAY);

you can change the color according to your requirement

and after this, put the below code in case MotionEvent.ACTION_UP: to change the color to default i.e transperent

v.setBackgroundColor(Color.TRANSPARENT);


来源:https://stackoverflow.com/questions/27597071/how-to-change-colored-to-black-and-whitegrayscale-when-touch-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!