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>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣的文章:Android自定义view实现圆形、圆角和椭圆图片(BitmapShader图形渲染)Android基础之使用Fragment控制切换多个页面android客户端从服务器端获取json数据并解析的实现代码Android 动画之ScaleAnimation应用详解android调试工具DDMS的使用详解Android按钮单击事件的四种常用写法总结Android的Activity跳转动画各种效果整理Android SQLite数据库增删改查操作的使用详解Android 动画之RotateAnimation应用详解Android基础之Fragment与Activity交互详解Android开发之SQLite的使用方法Android中判断网络连接是否可用及监控网络状态解析android中ProgressBar的用法android 调用系统的照相机和图库实例详解