當前位置:首頁 » 安卓系統 » android圓形圖片

android圓形圖片

發布時間: 2023-01-18 07:44:50

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);
}
}
}
}
熱點內容
安卓在哪裡找游戲 發布:2025-07-04 22:15:25 瀏覽:242
路由器訪問光貓 發布:2025-07-04 22:07:47 瀏覽:897
資料庫顯示語句 發布:2025-07-04 22:04:30 瀏覽:740
編程課道具 發布:2025-07-04 22:04:02 瀏覽:844
華為手機不是安卓什麼時候可以更新米加小鎮 發布:2025-07-04 22:01:37 瀏覽:785
飢荒伺服器搭建視頻 發布:2025-07-04 21:48:38 瀏覽:523
github上傳文件夾 發布:2025-07-04 21:29:22 瀏覽:1003
php課程學習中心 發布:2025-07-04 21:29:16 瀏覽:298
win7加密文件夾如何解密 發布:2025-07-04 21:25:24 瀏覽:555
為啥系統緩存的垃圾多呢 發布:2025-07-04 21:15:45 瀏覽:952