Android-自定义类实现图片圆角显示图片
程序员文章站
2022-04-01 10:44:48
Android-自定义类实现图片圆角显示图片圆片效果图圆角效果图代码展示MainActivity.javaactivity_main.xmlattrs.xmlShapedImageView.java在实际开发中我们经常会对例如标题栏,导航栏上的一些图片进行圆角处理圆片效果图圆角效果图代码展示MainActivity.javapublic class MainActivity extends Activity { @Override protected void onCre...
Android-自定义类实现图片圆角显示图片
在实际开发中我们经常会对例如标题栏,导航栏上的一些图片进行圆角处理
圆片效果图
圆角效果图
代码展示
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
注意修改com.example.xxx为自己的包路径
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.example.shapeimage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<com.example.shapeimage.ShapedImageView
android:id="@+id/image1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:src="@drawable/an"
test:shape_mode="circle" />
<com.example.shapeimage.ShapedImageView
android:id="@+id/image1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="20dp"
android:layout_centerInParent="true"
test:round_radius="20dp"
android:src="@drawable/an"
test:shape_mode="round_rect" />
</LinearLayout>
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ShapedImageView">
<attr name="shape_mode" format="enum">
<enum name="round_rect" value="1" />
<enum name="circle" value="2" />
</attr>
<attr name="round_radius" format="dimension" />
</declare-styleable>
</resources>
ShapedImageView.java
import java.util.Arrays;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.PorterDuff;
import android.graphics.drawable.shapes.RoundRectShape;
import android.graphics.drawable.shapes.Shape;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ImageView;
public class ShapedImageView extends ImageView {
private static final int SHAPE_MODE_ROUND_RECT = 1;
private static final int SHAPE_MODE_CIRCLE = 2;
private int mShapeMode = 0;
private float mRadius = 0;
private Shape mShape;
private Paint mPaint;
public ShapedImageView(Context context) {
super(context);
init(null);
}
public ShapedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public ShapedImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
private void init(AttributeSet attrs) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(LAYER_TYPE_HARDWARE, null);
}
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ShapedImageView);
mShapeMode = a.getInt(R.styleable.ShapedImageView_shape_mode, 0);
mRadius = a.getDimension(R.styleable.ShapedImageView_round_radius, 0);
a.recycle();
}
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setFilterBitmap(true);
mPaint.setColor(Color.BLACK);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (changed) {
switch (mShapeMode) {
case SHAPE_MODE_ROUND_RECT:
break;
case SHAPE_MODE_CIRCLE:
int min = Math.min(getWidth(), getHeight());
mRadius = (float) min / 2;
break;
}
if (mShape == null) {
float[] radius = new float[8];
Arrays.fill(radius, mRadius);
mShape = new RoundRectShape(radius, null, null);
}
mShape.resize(getWidth(), getHeight());
}
}
@Override
protected void onDraw(Canvas canvas) {
int saveCount = canvas.getSaveCount();
canvas.save();
super.onDraw(canvas);
switch (mShapeMode) {
case SHAPE_MODE_ROUND_RECT:
case SHAPE_MODE_CIRCLE:
if (mShape != null) {
mShape.draw(canvas, mPaint);
}
break;
}
canvas.restoreToCount(saveCount);
}
}
持续更新中…
本文地址:https://blog.csdn.net/weixin_44325444/article/details/107184889
上一篇: Android UI百分比布局
下一篇: 包里的电话都响了半天啦