繁体   English   中英

如何在单击之前在ImageButton中设置有效的图像

[英]How to set image with effect in ImageButton before to click

我想使用所选图像来设置具有效果的按钮,然后单击它,然后单击效果在ImageView中应用相同的图像,但是它的方式使用默认图像,而不是选择的图像。

    imgView = (ImageView) findViewById(R.id.imgView);
    btnSepia = (ImageButton) findViewById(R.id.btnSepia);

    BitmapDrawable abmp = (BitmapDrawable) imgView.getDrawable();
    bmp = abmp.getBitmap();

    mProgress = (ProgressBar) findViewById(R.id.progressBar);
    mProgress.setVisibility(View.INVISIBLE);

    Intent i = new Intent(
            Intent.ACTION_GET_CONTENT,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    i.setType("image/");

    startActivityForResult(i, RESULT_LOAD_IMAGE);


    final Drawable resSepia = new BitmapDrawable(getResources(), bmp);
    imgView.setImageDrawable(resSepia);

     Handler handler = new Handler();

    final Runnable r = new Runnable() {
        @Override
        public void run() {
            if (resSepia == null)
                return;
            final ColorMatrix matrixA = new ColorMatrix();
            // making image B&W
            matrixA.setSaturation(0);

            final ColorMatrix matrixB = new ColorMatrix();
            // applying scales for RGB color values
            matrixB.setScale(1f, .95f, .82f, 1.0f);
            matrixA.setConcat(matrixB, matrixA);

            final ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrixA);
            resSepia.setColorFilter(filter);

            btnSepia.setImageDrawable(resSepia);
        }
    };
    handler.postDelayed(r, 1000);

btnSepia.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) { 

            imgView.setImageDrawable(resSepia);

        }
    });

谢谢();

抱歉,我的英语不是我的母语。

我无法得到你的问题,关于我可以通过上面的描述得到什么,是你想要在行动上改变图像。

所以你可以使用Selector,基本上不要直接放置你的图像源而是在图像源中使用选择器。

要创建一个选择器,在资源中定义一个selector.xml,使用这种方式: <selector> (在xml中指定选择器标记),在它下面定义一个item标记,它将具有以下属性

<selector>
<item 
    android:state="<Give_State_HERE>" 
    android:drawable="<GIVE_Source_of_Your_Drawable_Here"/> 
</selector>

试试这个并指定这个xml作为你的img:source。希望这会有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM