加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 02:16:53 所属栏目:Java 来源:网络整理
导读:我想使用所选图像来设置具有效果的按钮,然后单击它,然后单击效果在 ImageView中应用相同的图像,但是它的方式使用默认图像,而不是选择的图像. imgView = (ImageView) findViewById(R.id.imgView); btnSepia = (ImageButton) findViewById(R.id.btnSepia); Bit
我想使用所选图像来设置具有效果的按钮,然后单击它,然后单击效果在 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.希望这会有所帮助

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读