当我有图像时按下RelativeLayout的效果

时间:2013-07-05 08:09:28

标签: android android-listview

对不起我的英文...这是一个Google传感器。

我正在关注本教程http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

我的问题是背景图片..在我的情况下,它不是一种颜色,而是一张SD卡的图像。工作良好。但是当我点击该项目并在我发布项目时返回其原始颜色时,我希望该图像变暗

我知道我可以用photoshop编辑另一个图像并按照教程进行操作,但随后必须创建许多图像,这需要很多时间和权重。也许可以用几行代码完成

3 个答案:

答案 0 :(得分:1)

假设您使用 ImageButton ,可以试试这个:

<ImageButton
    android:id="@+id/myImage"
    ...
    android:background="@null"
    android:contentDescription="@string/your_description" />

然后在Java代码中添加:

ImageButton button = (ImageButton) findViewById(R.id.myImage);
button.setOnTouchListener(this);

这是使你的图像变暗的代码(在这种情况下是变亮的,所以由你来找到正确的颜色):

@Override
public boolean onTouch(View v, MotionEvent event) {
    // Apply the pressed effect on a button
    ImageButton button = (ImageButton) v;
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        button.setColorFilter(Color.argb(150, 155, 155, 155));
        return false;
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        button.setColorFilter(Color.argb(0, 155, 155, 155));
        return false;
    }
    return super.onTouchEvent(event);
}

试一试,让我知道!

答案 1 :(得分:0)

尝试使用 ImageButton

,而不是使用 ImageView

答案 2 :(得分:0)

这就是代码

        Resources res = vi.getResources();
        Bitmap bitmap = BitmapFactory.decodeFile(item.getRutaImagen());
        final BitmapDrawable bd = new BitmapDrawable(res, bitmap);

        // ----------------------------------
        final ImageButton button = (ImageButton) vi.findViewById(R.id.imageButton);
        button.setBackgroundDrawable(bd);
        button.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    button.setColorFilter(Color.argb(150, 155, 155, 155));
                    Log.d("aaa","DOWN");
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    button.setColorFilter(Color.argb(0, 155, 155, 155));
                    Log.d("aaa","UP");
                }
                return true;
            }
        });

我得到“DOWN”和“UP”但图像按钮不会改变......

android:clickable="true"

此代码不延伸BaseAdapter的Activity ... extends