在Android中的ui上没有显示按钮单击效果(悬停)

时间:2015-05-06 15:38:30

标签: android

由于我需要圆形按钮,因此我使用以下代码

这是layout.xml文件中的按钮

<RoundedImageView
        android:id="@+id/btnCheck"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_launcher" />

为了渲染我正在使用以下类: -

public class RoundedImageView extends ImageView {

   public RoundedImageView(Context ctx, AttributeSet attrs) {
          super(ctx, attrs);
   }

   @Override
   protected void onDraw(Canvas canvas) {

          Drawable drawable = getDrawable();

          if (drawable == null) {
                 return;
          }

          if (getWidth() == 0 || getHeight() == 0) {
                 return;
          }
          Bitmap b = ((BitmapDrawable) drawable).getBitmap();
          Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

          int w = getWidth(), h = getHeight();

          Bitmap roundBitmap = getRoundedCroppedBitmap(bitmap, w);
          canvas.drawBitmap(roundBitmap, 0, 0, null);

   }

   public static Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius) {
          Bitmap finalBitmap;
          if (bitmap.getWidth() != radius || bitmap.getHeight() != radius)
                 finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius,
                              false);
          else
                 finalBitmap = bitmap;
          Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(),
                       finalBitmap.getHeight(), Config.ARGB_8888);
          Canvas canvas = new Canvas(output);

          final Paint paint = new Paint();
          final Rect rect = new Rect(0, 0, finalBitmap.getWidth(),
                       finalBitmap.getHeight());

          paint.setAntiAlias(true);
          paint.setFilterBitmap(true);
          paint.setDither(true);
          canvas.drawARGB(0, 0, 0, 0);
          paint.setColor(Color.parseColor("#BAB399"));
          canvas.drawCircle(finalBitmap.getWidth() / 2 + 0.7f,
                       finalBitmap.getHeight() / 2 + 0.7f,
                       finalBitmap.getWidth() / 2 + 0.1f, paint);
          paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
          canvas.drawBitmap(finalBitmap, rect, rect, paint);

          return output;
   }
}

在我的MainActivity类中,我声明了一个ImageView: -

private ImageView imageBtnCheck;

在OnCreate中我有以下代码,“check_icon”是drawable-hdpi文件夹中的.png文件。

imageBtnCheck = (ImageView) findViewById(R.id.btnCheck);
Bitmap iconCheck = BitmapFactory.decodeResource(getResources(),R.drawable.check_icon);          
imageBtnCheck.setImageBitmap(iconCheck);

Onclick代码如下,我在onCreate()中调用wireEventForBtnCheck: -

private void wireEventForBtnCheck() {
    imageBtnCheck.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
        //Code here
        }
    });
}

通过此代码,我可以根据需要显示带有图标的圆形按钮。而且当我点击按钮时,点击事件正在运行。但是UI中的点击效果(悬停)并没有发生,因为普通的android按钮会发生这种情况。

我是否添加了任何属性(或)代码以显示此点击效果以动态加载按钮?

2 个答案:

答案 0 :(得分:0)

当您设置可绘制的图像按钮时,悬停没有效果。所以你必须使用选择器作为你的图像按钮。例如,在drawable文件夹中创建选择器文件

  

ImageButtonSelector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" 
      android:drawable="@drawable/selected"/> <!-- pressed state -->
<item android:state_focused="true" 
      android:drawable="@drawable/focused"/> <!-- focused state -->
<item android:drawable="@drawable/default"/> <!-- default state -->

</selector>

然后将此选择器设置为图像按钮源

<RoundedImageView
    android:id="@+id/btnCheck"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/ImageButtonSelector" />

答案 1 :(得分:0)

您可以通过drawable xml实现此目的。在您的情况下,使用ic_launcher.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/ic_launcher_pressed"/>
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/ic_launcher_pressed"/>
    <item android:state_focused="true" android:drawable="@drawable/ic_launcher_selected"/>
    <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/ic_launcher_default"/>
</selector> 

或者您可以通过Java文件

进行设置
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable .addState(new int[] {android.R.attr.state_pressed},
    getResources().getDrawable(R.drawable.ic_launcher_pressed));
stateListDrawable .addState(new int[] {android.R.attr.state_focused},
    getResources().getDrawable(R.drawable.ic_launcher_selected));
stateListDrawable .addState(new int[] {},
    getResources().getDrawable(R.drawable.ic_launcher_default));
imageBtnCheck.setImageDrawable(stateListDrawable);