如何在点击之前为ImageButton设置带效果的图像

4

我想使用所选的图像来设置按钮并产生对应效果,点击按钮之前同步更新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);

        }
    });

感谢();

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

1个回答

0

我无法理解你的问题,根据上述描述,我认为你想在某个操作后更改图像。

因此,您可以使用选择器,基本上不要直接放置图像源,而是在图像源中使用选择器。

要创建一个选择器,请在资源中定义一个selector.xml文件,使用以下方式:<selector>(在xml中指定selector标记),然后在其中定义一个item标记,该标记将具有以下属性

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

尝试这个,并将这个xml指定为您的img:source,希望能帮到你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接