Android 网络请求的图片用自定义圆形View展示

Android 网络请求的图片用自定义圆形View展示:
本来借鉴简书上的一篇文章,侵删,地址http://www.jianshu.com/p/6b5eef0f6f3d
百度快照地址:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c42246124662e8bd24241402d0d82f2747f41802bded602571507be9dad58f4bdeb0982c2a8b33712d5cd04e05a26fb8bb3732b024872998b81897ad814484ddd8c4af5f44be25127bf0e7fb5e1760bd78f1642693a08e39624811cafa4065e8287c3ee92057c24ceee14279778ae1ac2d5bb32ec7121b80ae45a74e66a264d2081a5453d046a67e25313190585688534e7785ea2df05b735754b35fb2c9c6b4fc49fc8aa826988bdcde598213ecd29af266360645fb55a9ddb8a54b625203a9ddae12d341b6ed9aba5b9e13c0112cfe06216a7ccf1b83f3fb4084644d85947f8e5e3527581daec4638c06676e41bf481ea66fc062c78c613aabfd9cf8886433b8e9b46406fabacc33&p=882a9545d7df2de40be2962c500c86&newp=882a9545d7d506dd07be9b7c58598d231610db2151d7d7136b82c825d7331b001c3bbfb42324150ed7c07d6703ae4e5decf03773330123a3dda5c91d9fb4c574799e60462f&user=baidu&fm=sc&query=%CD%BC%C6%AC%CD%A8%B9%FD%B6%FE%B4%CE%B2%C9%D1%F9%B9%CC%B6%A8%BF%ED%B8%DF%CE%AA%C8%E750dp%2C%CD%A8%B9%FD%D7%D4%B6%A8%D2%E5View%CF%D4%CA%BE%CE%AA%D4%B2%D0%CE%CD%BC%C6%AC&qid=85d2529900000061&p1=1

它是做什么呢?(先看张效果图)
这里写图片描述

这就是我使用自定义View 加载网络请求的图片,显示的为圆形。

如何操作呢?

①建一个CircleImageView类,继承ImageView
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Created by Dabin 
 */

public class CircleImageView extends ImageView {
    private Paint mPaint; //画笔

    private int mRadius; //圆形图片的半径

    private float mScale; //图片的缩放比例

    public CircleImageView(Context context) {
        super(context);
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //因为是圆形图片,所以应该让宽高保持一致
        int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
        mRadius = size / 2;

        setMeasuredDimension(size, size);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        mPaint = new Paint();
        Bitmap bitmap = drawableToBitmap(getDrawable());

        //初始化BitmapShader,传入bitmap对象
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

        //计算缩放比例
        mScale = (mRadius * 2.0f) / Math.min(bitmap.getHeight(), bitmap.getWidth());

        Matrix matrix = new Matrix();
        matrix.setScale(mScale, mScale);
        bitmapShader.setLocalMatrix(matrix);


        mPaint.setShader(bitmapShader);

        //画圆形,指定好中心点坐标、半径、画笔
        canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
    }

    //写一个drawble转BitMap的方法
    private Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bd = (BitmapDrawable) drawable;
            return bd.getBitmap();
        }
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        drawable.draw(canvas);
        return bitmap;
    }
}
②使用自定义View,在布局文件中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.bwie.www.yuekaoa.CircleImageView
        android:id="@+id/img_itema"
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="40dp"
        android:layout_marginTop="28dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/song_itema"
        android:text="歌名:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_alignTop="@+id/img_itema"
        android:layout_toEndOf="@+id/img_itema" />

    <TextView
        android:id="@+id/name_itema"
        android:text="我的啊"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/img_itema"
        android:layout_alignStart="@+id/song_itema" />


</RelativeLayout>

然后一运行就会出现如上图所显示结果。

③其中的方法,大家可以去原文处,看看详细讲解。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值