As I need rounded buttons I am using the following code
因为我需要圆形按钮,我使用以下代码
Here is my button in layout.xml file
这是我在layout.xml文件中的按钮
<RoundedImageView
android:id="@+id/btnCheck"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_launcher" />
For this to render I am using the below class:-
为此渲染我正在使用以下类: -
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;
}
}
In my MainActivity class i have declared a ImageView :-
在我的MainActivity类中,我声明了一个ImageView: -
private ImageView imageBtnCheck;
In OnCreate I have the below code and "check_icon" is the .png file in drawable-hdpi folder.
在OnCreate中,我有以下代码,“check_icon”是drawable-hdpi文件夹中的.png文件。
imageBtnCheck = (ImageView) findViewById(R.id.btnCheck);
Bitmap iconCheck = BitmapFactory.decodeResource(getResources(),R.drawable.check_icon);
imageBtnCheck.setImageBitmap(iconCheck);
The Onclick code is as follows and I am calling wireEventForBtnCheck in onCreate() :-
Onclick代码如下,我在onCreate()中调用wireEventForBtnCheck: -
private void wireEventForBtnCheck() {
imageBtnCheck.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//Code here
}
});
}
By this code I am able to display round button with the icon in it as need. And also when I click on the button the click event is working. But the click effect (hover) in the UI is not happening as it happens for normal android button.
通过这段代码,我可以根据需要显示带有图标的圆形按钮。而且当我点击按钮时,点击事件正在运行。但是UI中的点击效果(悬停)没有发生,因为它发生在普通的android按钮上。
Is there any property (or) any kind code that I have add to show this click effect for dynamically loading buttons?
是否有任何属性(或)我添加的任何类型的代码显示此动态加载按钮的单击效果?
2 个解决方案
#1
when you are setting the drawable imagebutton hover didnt effect. so you have to use the Selector for your image button. For example create selector file in drawable folder
当你设置drawable imagebutton悬停没有效果。所以你必须使用选择器作为你的图像按钮。例如,在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>
and then set this selector to your image button source
然后将此选择器设置为图像按钮源
<RoundedImageView
android:id="@+id/btnCheck"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ImageButtonSelector" />
#2
You can achieve this by drawable xml.In your case ic_launcher.xml
您可以通过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>
or you can set it through your Java file
或者您可以通过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);
#1
when you are setting the drawable imagebutton hover didnt effect. so you have to use the Selector for your image button. For example create selector file in drawable folder
当你设置drawable imagebutton悬停没有效果。所以你必须使用选择器作为你的图像按钮。例如,在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>
and then set this selector to your image button source
然后将此选择器设置为图像按钮源
<RoundedImageView
android:id="@+id/btnCheck"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ImageButtonSelector" />
#2
You can achieve this by drawable xml.In your case ic_launcher.xml
您可以通过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>
or you can set it through your Java file
或者您可以通过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);