zoukankan      html  css  js  c++  java
  • 将头图片变成圆形简单实现

    package com.loaderman.customviewdemo;
    
    import android.content.Context;
    import android.graphics.*;
    import android.util.AttributeSet;
    import android.view.View;
    
    
    public class AvatorView extends View {
        private Paint mPaint;
        private Bitmap mBitmap;
        private BitmapShader mBitmapShader;
    
        public AvatorView(Context context, AttributeSet attrs) throws Exception{
            super(context, attrs);
            mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.avator);
            mPaint = new Paint();
            mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Matrix matrix = new Matrix();
            float scale = (float) getWidth()/mBitmap.getWidth();
            matrix.setScale(scale,scale);
            mBitmapShader.setLocalMatrix(matrix);//设置坐标变换矩阵
            mPaint.setShader(mBitmapShader);
    
            float half = getWidth()/2;
            canvas.drawCircle(half,half,getWidth()/2,mPaint);
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:background="@android:color/white"
        >
    
    
     <com.loaderman.customviewdemo.AvatorView
         android:layout_width="200dp"
         android:layout_height="200dp" />
    </LinearLayout>

  • 相关阅读:
    锚的应用
    有关于MP3音频文件的编码解码资料吗
    自定义web.config配置节 (转)
    HTC 文件
    Asp.Net音频文件上传和播放
    dotnet下用c#编写下载器
    自动滚屏代码
    agsXMPP分析:agsXMPP Namespace
    Socket网络编程学习笔记(1)
    (♂)程序打包工具setup2go使用教程
  • 原文地址:https://www.cnblogs.com/loaderman/p/10213019.html
Copyright © 2011-2022 走看看