通过style控制圆形imageView显示

1.

 

2.drawable--style

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 图层1(oval) -->

    <!-- left,top,right,bottom定义为-10,是为了扩大oval,达到覆盖四角的效果 -->

    <item android:left="-10dp" android:top="-10dp" android:right="-10dp" android:bottom="-10dp">

        <shape

            android:shape="oval">

            <!-- oval_inner[内部] -->

            <solid android:color="#F00" />

            <!-- oval_outer[边线] ,使用时改成父控件颜色即可 ,图中的蓝色背景 -->

            <stroke

                android:width="10dp"

                android:color="#00F" />  

            <!-- oval_inner_size[大小(除去边线)] ,也是最终裸露出来的圆形图像区域-->

            <size

                android:height="50dp"

                android:width="50dp" />

            <!-- 使oval_inner透明,裸露出将来设置的背景图片 -->

            <gradient android:centerColor="#0000" />

        </shape>

    </item>

</layer-list>

3.

<ImageView

        android:id="@+id/iv_circle_header"

        android:layout_width="100dp"

        android:layout_height="100dp"

        android:background="@mipmap/head"

        android:src="@drawable/image_style"/>

    <!-- 普通头像-->

    <ImageView

        android:layout_below="@+id/iv_circle_header"
        android:layout_width="100dp"

        android:layout_height="100dp"

        android:background="@mipmap/head"/>

 

转载于:https://www.cnblogs.com/galibujianbusana/p/6206489.html

1.创建一个圆形ImageView类 ``` public class CircleImageView extends ImageView { private Paint borderPaint; private int borderWidth; private int borderColor; public CircleImageView(Context context, AttributeSet attrs) { super(context, attrs); borderPaint = new Paint(); borderPaint.setAntiAlias(true); borderPaint.setStyle(Paint.Style.STROKE); borderWidth = 2; borderColor = Color.WHITE; } @Override protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); if (drawable == null) { super.onDraw(canvas); return; } if (getWidth() == 0 || getHeight() == 0) { return; } Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); Bitmap bitmapScaled = Bitmap.createScaledBitmap(bitmap, getWidth(), getHeight(), false); Bitmap bitmapCircle = getCircleBitmap(bitmapScaled); canvas.drawBitmap(bitmapCircle, 0, 0, null); borderPaint.setStrokeWidth(borderWidth); borderPaint.setColor(borderColor); canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2 - borderWidth / 2, borderPaint); } private Bitmap getCircleBitmap(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = bitmap.getWidth() / 2; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } public void setBorderWidth(int borderWidth) { this.borderWidth = borderWidth; invalidate(); } public void setBorderColor(int borderColor) { this.borderColor = borderColor; invalidate(); } } ``` 2.在布局文件中添加CircleImageView ``` <com.example.CircleImageView android:id="@+id/circleImageView" android:layout_width="200dp" android:layout_height="200dp" android:layout_centerInParent="true" android:src="@drawable/arrow" app:borderColor="@color/colorAccent" app:borderWidth="4dp" /> ``` 3.在Activity中设置点击事件,用动画旋转圆形ImageView ``` public class MainActivity extends AppCompatActivity { private CircleImageView circleImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); circleImageView = findViewById(R.id.circleImageView); circleImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startRotateAnimation(); } }); } private void startRotateAnimation() { RotateAnimation rotateAnimation = new RotateAnimation(0, 360, circleImageView.getWidth() / 2, circleImageView.getHeight() / 2); rotateAnimation.setDuration(3000); rotateAnimation.setInterpolator(new AccelerateDecelerateInterpolator()); rotateAnimation.setFillAfter(true); circleImageView.startAnimation(rotateAnimation); } } ``` 4.效果演示 ![circle_image_view.gif](https://cdn.jsdelivr.net/gh/anhaochen/source/circle_image_view.gif)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值