欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 实现切圆图作为头像使用实例

程序员文章站 2024-02-25 22:52:45
android 切圆图 效果图如下: myview 类 public class myview extends view { bitmap bm...

android 切圆图

效果图如下:

Android 实现切圆图作为头像使用实例

myview 类

public class myview extends view {

  bitmap bmp;

  paint paint = new paint();


  public myview(context context) {
    super(context);
  }

  public myview(context context, attributeset attrs) {
    super(context, attrs);
    bmp = bitmapfactory.decoderesource(getresources(), r.mipmap.c);
    src = new rectf(bmp.getwidth() / 2 - 50, bmp.getheight() / 2 - 50, bmp.getwidth() / 2 + 50, bmp.getheight() / 2 + 50);
    dst = new rect(200, 200, 400, 400);

    paint.setantialias(true);
    paint.setdither(true);
    shader shaer = new bitmapshader(bmp, shader.tilemode.mirror, shader.tilemode.repeat);
    paint.setshader(shaer);
  }


  private rectf src = null;
  private rect dst = null;

  @override
  protected void ondraw(canvas canvas) {
    super.ondraw(canvas);
    //绘制bitmap

    matrix m = new matrix();

    //每次set都会重置矩形
    m.setrotate(90, bmp.getwidth() / 2, bmp.getheight() / 2);

    m.posttranslate(100, 100);

    m.prescale(0.5f, 0.5f, bmp.getwidth() / 2, bmp.getheight() / 2);

    //错切
    m.postskew(0.3f, 0.3f);

    // canvas.drawbitmap(bmp, m, null);

    // canvas.drawbitmap(bmp, src, dst, null);

    //拿view的高宽
    canvas.drawarc(src, 100, 270, true, paint);

  }
}

mainactivity 类

public class mainactivity extends appcompatactivity {

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
  }
}

xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.example.administrator.lesson12_drawbitmap.mainactivity">


  <com.example.administrator.lesson12_drawbitmap.myview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


</linearlayout>

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!