Android-custom class to achieve picture rounded corners to display pictures

Android-custom class to achieve picture rounded corners to display pictures

In actual development, we often round corners of some pictures on the title bar and navigation bar.

Disc effect picture

Insert picture description here

Rounded corner rendering

Insert picture description here

Code display

MainActivity.java

public class MainActivity extends Activity {
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
}

activity_main.xml

Pay attention to modify com.example.xxx to your own package path

<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);
    }
 
}

Continuously updating...

Guess you like

Origin blog.csdn.net/weixin_44325444/article/details/107184889