android圆形图片
1. Android设置图片圆角的方法
Android中经常会遇到对图片进行二次处理,例如加圆角,或者显示圆形图片
通过第三方框架Glide实现图片显示有圆角,有三种写法如下:
1.1、第一种实现:
1.2、第二种实现:
1.3、第三种实现:
自定义ImageView:
对图片进行处理,此方法还可以加边框
实现圆形和边框:
以上就是本文的全部内容,希望对大家的学习有所帮助!
2. android圆形头像怎么实现
就是一个正方形的图片,通过自定义控件,将这张图片裁剪重绘,得到一张圆形的图片。我这里有代码。我给你找找去。
3. android 开发 imgview 怎么弄成圆形
imageview的属性中可以加入background来定义它的背景,将背景定义成一个圆形的drawable就可以了。
另一种办法,也可以自己定义一个view来显示圆形的图片,你可以参考http://blog.csdn.net/alan_biao/article/details/17379925来进行设计。
4. android中 怎么显示一直图片为圆形图片
android中的imageview只能显示矩形的图片,这样一来不能满足我们其他的需求,比如要显示圆形的图片,这个时候,我们就需要自定义imageview了,其原理就是首先获取到图片的bitmap,然后进行裁剪圆形的bitmap,然后在ondraw()进行绘制圆形图片输出。
5. android中如何实现图片成一个圆形排列
1、在设置中更改外观,只有部分手机支持
2、下载桌面美化软件,更改风格
3、刷机,即ROOT
(注:刷机要注意安全,手机很可能报废)
6. android swiperefreshlayout 为什么是圆形的
其实主要靠:paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));这行代码,为什么呢,我给大家解释下,SRC_IN这种模式,两个绘制的效果叠加后取交集展现后图,怎么说呢,咱们第一个绘制的是个圆形,第二个绘制的是个Bitmap,于是交集为圆形,展现的是BItmap,就实现了圆形图片效果。圆角,其实就是先绘制圆角矩形,是不是很简单,以后别人再说实现圆角,你就把这一行代码给他就行了。
从Android的示例中,给大家证明一下:
下面有一张PorterDuff.Mode的16中效果图,咱们的只是其一:
源码咱们只关心谁先谁后绘制的:
[java] view plain
canvas.translate(x, y);
canvas.drawBitmap(mDstB, 0, 0, paint);
paint.setXfermode(sModes[i]);
canvas.drawBitmap(mSrcB, 0, 0, paint);
paint.setXfermode(null);
canvas.restoreToCount(sc);
可以看出先绘制的Dst,再绘制的Src,最后的展示是SrcIn那个图:显示的区域是二者交集,展示的是Src(后者)。和咱们前面结论一致。效果16种,大家可以自由组合展示不同的效果。
好了,原理和核心代码解释完成。下面开始写自定义View。
1、自定义属性:
[html] view plain
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="borderRadius" format="dimension" />
<attr name="type">
<enum name="circle" value="0" />
<enum name="round" value="1" />
</attr>
<attr name="src" format="reference"></attr>
<declare-styleable name="CustomImageView">
<attr name="borderRadius" />
<attr name="type" />
<attr name="src" />
</declare-styleable>
</resources>
2、构造中获取自定义的属性:
[java] view plain
/**
* TYPE_CIRCLE / TYPE_ROUND
*/
private int type;
private static final int TYPE_CIRCLE = 0;
private static final int TYPE_ROUND = 1;
/**
* 图片
*/
private Bitmap mSrc;
/**
* 圆角的大小
*/
private int mRadius;
/**
* 控件的宽度
*/
private int mWidth;
/**
* 控件的高度
*/
private int mHeight;
public CustomImageView(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public CustomImageView(Context context)
{
this(context, null);
}
/**
* 初始化一些自定义的参数
*
* @param context
* @param attrs
* @param defStyle
*/
public CustomImageView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageView, defStyle, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++)
{
int attr = a.getIndex(i);
switch (attr)
{
case R.styleable.CustomImageView_src:
mSrc = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0));
break;
case R.styleable.CustomImageView_type:
type = a.getInt(attr, 0);// 默认为Circle
break;
case R.styleable.CustomImageView_borderRadius:
mRadius= a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f,
getResources().getDisplayMetrics()));// 默认为10DP
break;
}
}
a.recycle();
}
3、onMeasure中获取控件宽高:
[java] view plain
/**
* 计算控件的高度和宽度
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
/**
* 设置宽度
*/
int specMode = MeasureSpec.getMode(widthMeasureSpec);
int specSize = MeasureSpec.getSize(widthMeasureSpec);
if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate
{
mWidth = specSize;
} else
{
// 由图片决定的宽
int desireByImg = getPaddingLeft() + getPaddingRight()
+ mSrc.getWidth();
if (specMode == MeasureSpec.AT_MOST)// wrap_content
{
mWidth = Math.min(desireByImg, specSize);
} else
mWidth = desireByImg;
}
/***
* 设置高度
*/
specMode = MeasureSpec.getMode(heightMeasureSpec);
specSize = MeasureSpec.getSize(heightMeasureSpec);
if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate
{
mHeight = specSize;
} else
{
int desire = getPaddingTop() + getPaddingBottom()
+ mSrc.getHeight();
if (specMode == MeasureSpec.AT_MOST)// wrap_content
{
mHeight = Math.min(desire, specSize);
} else
mHeight = desire;
}
setMeasuredDimension(mWidth, mHeight);
}
4、根据Type绘制:
[java] view plain
/**
* 绘制
*/
@Override
protected void onDraw(Canvas canvas)
{
switch (type)
{
// 如果是TYPE_CIRCLE绘制圆形
case TYPE_CIRCLE:
int min = Math.min(mWidth, mHeight);
/**
* 长度如果不一致,按小的值进行压缩
*/
mSrc = Bitmap.createScaledBitmap(mSrc, min, min, false);
canvas.drawBitmap(createCircleImage(mSrc, min), 0, 0, null);
break;
case TYPE_ROUND:
canvas.drawBitmap(createRoundConerImage(mSrc), 0, 0, null);
break;
}
}
/**
* 根据原图和变长绘制圆形图片
*
* @param source
* @param min
* @return
*/
private Bitmap createCircleImage(Bitmap source, int min)
{
final Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888);
/**
* 产生一个同样大小的画布
*/
Canvas canvas = new Canvas(target);
/**
* 首先绘制圆形
*/
canvas.drawCircle(min / 2, min / 2, min / 2, paint);
/**
* 使用SRC_IN,参考上面的说明
*/
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
/**
* 绘制图片
*/
canvas.drawBitmap(source, 0, 0, paint);
return target;
}
/**
* 根据原图添加圆角
*
* @param source
* @return
*/
private Bitmap createRoundConerImage(Bitmap source)
{
final Paint paint = new Paint();
paint.setAntiAlias(true);
Bitmap target = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(target);
RectF rect = new RectF(0, 0, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rect, mRadius, mRadius, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(source, 0, 0, paint);
return target;
}
好了,我就不解析代码了,自定义View这是第五篇了,,,,写得好恶心,,,,
7. android圆形灰色图片做按钮背景,圆形背景上需要叠加一张图片和文字,文字要在图片的下方,怎么实现
用android:drawableTop设置图片就好了。
8. Android开发中的圆角图片+圆形图片,看这一篇就够了!
最近在苦练Kotlin,一款不错的app(开眼)中的布局吸引了我,也在不懈的努力下通过Kotlin语言完成了开眼首页的RecyclerView多ViewType布局效果,开心!(文末会贴出来)其中用到的图片处理控件很实用,也在日常的项目中会经常用到,因此安利给大家!
本次就针对Android开发中的图片处理给大家安利两个开源库:CircleImageView(圆形图片)+RoundedImageView(圆角图片)。
Tips: Kotlin实现,写完整个项目会分享git源码给大家!
没错,这是一个RecyclerView布局实现的~
9. android 怎么把头像统一成圆形
//给你一个工具类,是把bitmap类型的图片转换成圆形图片:
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
public class BmpToRound {
/**
* 转换图片成圆形
* @param bitmap 传入Bitmap对象
* @return
*/
public static Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;
if (width <= height) {
roundPx = width / 2;
top = 0;
bottom = width;
left = 0;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width,
height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int)left, (int)top, (int)right, (int)bottom);
final Rect dst = new Rect((int)dst_left, (int)dst_top, (int)dst_right, (int)dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
return output;
}
}
10. android 如何把正方形图片显示圆形
Android应用开发中,很多头像都要求显示成圆形的,这就可以使用android的canvas、paint这些类来进行设置圆形,先设置paint的样式为圆形,然后把你要设置成圆形的图片重新赋值给paint这个类:canvas.drawBitmap(tempBmp, rect, rect, paint);
核心代码如下(引用这位前辈:http://blog.sina.com.cn/s/blog_7607703f0101dhlj.html,我增加一些注释,原来是没有注释):
packagecom.liang.round;
importandroid.annotation.SuppressLint;
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.graphics.Bitmap.Config;
importandroid.graphics.BitmapFactory;
importandroid.graphics.Canvas;
importandroid.graphics.Paint;
importandroid.graphics.PorterDuff;
importandroid.graphics.PorterDuffXfermode;
importandroid.graphics.Rect;
importandroid.view.View;
publicclassMyViewextendsView{
privateBitmapbmp=null;
privatePaintpaint=null;
publicMyView(Contextcontext){
super(context);
//TODOAuto-generatedconstructorstub
paint=newPaint();//实例化画笔类
BitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeResource(context.getResources(),R.drawable.test,options);//获得你存放在drawable下的正方形图片
options.inJustDecodeBounds=false;
BitmaptempBmp=BitmapFactory.decodeResource(context.getResources(),R.drawable.test,options);//实例化一个bitmap图片类
intwidth=options.outWidth;
intheight=options.outHeight;
intsize=width>height?height:width;//边框
intpos=(int)(size/2);
doubleradius=pos*Math.sin(45*180/Math.PI);//半径
size=(int)(radius*2);
pos=(int)(size/2);
bmp=Bitmap.createBitmap(size,size,Config.ARGB_8888);
Canvascanvas=newCanvas(bmp);
Rectrect=newRect(0,0,size,size);
paint.setAntiAlias(true);
canvas.drawCircle(pos,pos,(float)radius,paint);
paint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
canvas.drawBitmap(tempBmp,rect,rect,paint);
tempBmp.recycle();
}
@SuppressLint("DrawAllocation")
@Override
protectedvoidonDraw(Canvascanvas){
//TODOAuto-generatedmethodstub
super.onDraw(canvas);
if(bmp!=null){
if(!bmp.isRecycled()){
canvas.drawBitmap(bmp,100,100,paint);
}
}
}
}