如何在安卓中触摸时将彩色更改为黑白(灰度)



>我有水平布局,动态添加图像视图,使用线性布局添加图像。 和所有水平视图垂直添加斯科尔视图

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

喜欢这个

现在,如果我在触摸水平扫描视图时,我想将图像着色更改为灰度(黑白(。在水平上触摸 斯科尔视图 推杆,但无法调和。并且不会改变效果。我做什么。

我的主要意图是:当水平滚动图像时,只有窗口视图(触摸视图(灰色的 ohters 保持前一阶段(彩色(。

这是我的代码适用于图像,但我想在水平视图上工作。

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;
    }

您可以在 xml 文件中添加布局背景,并为背景设置自定义布局,如下所示

android:background="@drawable/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>

希望对您有所帮助:)

基本上,您的要求是在触摸图像视图时更改它的颜色,为此您必须在case MotionEvent.ACTION_DOWN:中实现颜色更改代码例如,变化的颜色是浅灰色所以代码将是

v.setBackgroundColor(Color.LTGRAY);

您可以根据需要更改颜色

在此之后,将以下代码放入case MotionEvent.ACTION_UP:将颜色更改为默认颜色,即透明

v.setBackgroundColor(Color.TRANSPARENT);

最新更新