实现图片下凹效果

这里写图片描述

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

<?xml version="1.0" encoding="utf-8"?>
<resources>

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

    <!--<declare-styleable name="ArcView">-->
        <!--<attr name="arcHeight"/>-->
        <!--<attr name="bgColor" format="color"/>-->
    <!--</declare-styleable>-->

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

</resources>

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

  <atyh.road.ArcImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:arcHeight="150dp"
        android:scaleType="center"
        android:src="@drawable/test3"
        />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值