Android RoundedBitmapDrawable:Android官方的圆角图形图象实现方案
2016-07-08 15:21
501 查看
RoundedBitmapDrawable是Android在support v4的扩展包中新增的实现圆角图形的关键类,借助RoundedBitmapDrawable的帮助,可以轻松的以Android标准方式实现圆角图形图象。
写一个简单的例子。
写一个线性布局,竖直方向放三个ImageView:
[html] view
plain copy
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="zhangphil.app.MainActivity">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" />
</LinearLayout>
然后通过Java代码分别放原始图,圆角图,圆形图作为对比:
[java] view
plain copy
package zhangphil.app;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//未做圆角处理的原始图
ImageView image1 = (ImageView) findViewById(R.id.imageView1);
image1.setImageResource(R.drawable.zhangphil);
//圆角图
RoundedBitmapDrawable roundedDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.zhangphil));
roundedDrawable.getPaint().setAntiAlias(true);
roundedDrawable.setCornerRadius(30);
ImageView image2 = (ImageView) findViewById(R.id.imageView2);
image2.setImageDrawable(roundedDrawable);
//圆形图
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.zhangphil);
RoundedBitmapDrawable circleDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.zhangphil));
circleDrawable.getPaint().setAntiAlias(true);
circleDrawable.setCornerRadius(Math.max(bitmap.getWidth(), bitmap.getHeight()));
ImageView image3 = (ImageView) findViewById(R.id.imageView3);
image3.setImageDrawable(circleDrawable);
}
}
代码运行结果:
附录文章:
1,《Android圆形头像图Circle ImageView》链接地址:http://blog.csdn.net/zhangphil/article/details/44960551
2,《Android CustomShapeImageView对图片进行各种样式裁剪:圆形、星形、心形、花瓣形等》链接地址:http://blog.csdn.net/zhangphil/article/details/50119921
写一个简单的例子。
写一个线性布局,竖直方向放三个ImageView:
[html] view
plain copy
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="zhangphil.app.MainActivity">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" />
</LinearLayout>
然后通过Java代码分别放原始图,圆角图,圆形图作为对比:
[java] view
plain copy
package zhangphil.app;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//未做圆角处理的原始图
ImageView image1 = (ImageView) findViewById(R.id.imageView1);
image1.setImageResource(R.drawable.zhangphil);
//圆角图
RoundedBitmapDrawable roundedDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.zhangphil));
roundedDrawable.getPaint().setAntiAlias(true);
roundedDrawable.setCornerRadius(30);
ImageView image2 = (ImageView) findViewById(R.id.imageView2);
image2.setImageDrawable(roundedDrawable);
//圆形图
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.zhangphil);
RoundedBitmapDrawable circleDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.zhangphil));
circleDrawable.getPaint().setAntiAlias(true);
circleDrawable.setCornerRadius(Math.max(bitmap.getWidth(), bitmap.getHeight()));
ImageView image3 = (ImageView) findViewById(R.id.imageView3);
image3.setImageDrawable(circleDrawable);
}
}
代码运行结果:
附录文章:
1,《Android圆形头像图Circle ImageView》链接地址:http://blog.csdn.net/zhangphil/article/details/44960551
2,《Android CustomShapeImageView对图片进行各种样式裁剪:圆形、星形、心形、花瓣形等》链接地址:http://blog.csdn.net/zhangphil/article/details/50119921
相关文章推荐
- Android TextView通过Spannable识别超链接、邮箱、电话
- 图吧地图夜间导航SDK接口应用案例
- 安卓Android Context类实例详解
- Android 常见的抓log方法总结
- Android Message 及其使用
- Android使用 format,将数字变为两位数
- AndroidVolley
- android之自定义View和ViewGroup(一)(此篇不讲述代码,只讲述原理和结构,带你走进自定义的世界)
- [android]手把手通过一个类实现侧滑退出activity功能
- Android基础知识(2)—事件处理
- [置顶] Android通讯录字母排序城市列表展示效果
- 用android模拟器Genymotion定位元素
- android系统文件的权限
- 图吧地图导航SDK在安卓上的应用案例
- Android开发入门之Notification用法分析
- Android Handler Message总结
- 【转】第一次使用Android Studio时你应该知道的一切配置(二):新建一个属于自己的工程并安装Genymotion模拟器
- Android实时获取当前下载速度
- 一篇文章看懂Android学习最佳路线
- 安卓四大控件之Service