实现图片下凹效果

实现图片下凹效果_第1张图片

1新建自定义ImageView

package atyh.road;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ImageView;

@SuppressLint("AppCompatCustomView")
public class ArcImageView extends ImageView{
    private int mArcHeight;
    private static final String TAG = "ArcImageView";
    public ArcImageView(Context context) {
        this(context, null);
    }
    public ArcImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public ArcImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ArcImageView);
        mArcHeight = typedArray.getDimensionPixelSize(R.styleable.ArcImageView_arcHeight, 0);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        Path path = new Path();
        path.moveTo(0,0);
        path.lineTo(0, getHeight() - mArcHeight);  
        path.quadTo(getWidth() / 2, getHeight(), getWidth(), getHeight() - mArcHeight);
        path.lineTo(getWidth(), 0);
        path.close();
        canvas.clipPath(path);     
        super.onDraw(canvas);
    }
}

2.res->value->attrs.xml


<resources>

    <attr name="arcHeight" format="dimension"/>

    
        
        
    

    <declare-styleable name="ArcImageView">
        <attr name="arcHeight"/>
    declare-styleable>

resources>

3.在layout中导入布局,areHeight值越大,下凹的弧度越大

  .road.ArcImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:arcHeight="150dp"
        android:scaleType="center"
        android:src="@drawable/test3"
        />

你可能感兴趣的:(Android)