[英]Button click effect (hover) is not showing on ui in 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
}
});
}
通过此代码,我能够根据需要显示带有图标的圆形按钮。 而且,当我单击按钮时,单击事件也在起作用。 但是用户界面中的点击效果(悬停)并未发生,因为它发生在普通的android按钮上。
是否添加了任何属性(或)任何种类的代码,以显示动态加载按钮的点击效果?
设置可绘制图像按钮时,悬停效果不佳。 因此您必须对图像按钮使用选择器。 例如在可绘制文件夹中创建选择器文件
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" />
您可以通过可绘制的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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.