安卓如何有圓形外框
1. android 圓角邊框 陰影邊框怎麼設置
所謂添加陰影,就是兩個畫布從重疊,上方的畫布小於下方的畫布,陰影顏色為下方的畫布的顏色。
item 中shape 的屬性 (rectangle:矩形;line:線性;oval:橢圓;ring:環形),默認為矩形
corners //設置圓角幅度,必須是在shape=rectangle的時候,corners才有效
<corners
Android:radius="dimension" //全部的圓角半徑
android:topLeftRadius="dimension" //左上角的圓角半徑
android:topRightRadius="dimension" //右上角的圓角半徑
android:bottomLeftRadius="dimension" //左下角的圓角半徑
android:bottomRightRadius="dimension" /> //右下角的圓角半徑
eg:<corners android:radius="10dp" />
solid用以指定內部填充色
e.g:<solid android:color="color" />
gradient //定義漸變色,可以定義兩色漸變和三色漸變,及漸變樣式
linear(線性漸變)、radial(放射性漸變)、sweep(掃描式漸變), 在構造放射性漸變時,要加上android:gradientRadius屬性(漸變半徑),即必須指定漸變半徑的大小才會起作用。
<gradient
android:type=["linear" | "radial" | "sweep"] //共有3中漸變類型
android:angle="integer" //漸變角度,必須為45的倍數,0為從左到右,90為從上到下
android:centerX="float" //漸變中心X的相當位置,范圍為0~1
android:centerY="float" //漸變中心Y的相當位置,范圍為0~1
android:startColor="color" //漸變開始點的顏色
android:centerColor="color" //漸變中間點的顏色,在開始與結束點之間
android:endColor="color" //漸變結束點的顏色
android:gradientRadius="float" //漸變的半徑,只有當漸變類型為radial時才有效
android:useLevel=["true" | "false"] /> //使用LevelListDrawable時就要設置為true。設為false時才有漸變效果
stroke //這是描邊屬性,可以定義描邊的寬度,顏色,虛實線等
<stroke
android:width="dimension" //描邊的寬度
android:color="color" //描邊的顏色 // 以下兩個屬性設置虛線
android:dashWidth="dimension" //虛線的寬度,值為0時是實線
android:dashGap="dimension" /> //虛線的間隔
2. Android:如下關於繪制圓角矩形邊框問題,怎麼解決
paint.setAntiAlias(true);
嘗試在畫筆上設置抗鋸齒
3. 我想在Android的一個網格視圖中顯示圖片,並對這些圖片添加一個圓角矩形邊框,我該怎麼做
移動工具 【V】 直接選取工具、組選取工具 【A】 鋼筆、添加錨點、刪除錨點、改變路徑角度 【P】 添加錨點工具 【+】 刪除錨點工具 【-】 文字、區域文字、路徑文字、豎向文字、豎向區域文字、豎向路徑文字 【T】 橢圓、多邊形、星形、螺旋形 【L】 增加邊數、倒角半徑及螺旋圈數(在【L】、【M】狀態下繪圖) 【↑】 減少邊數、倒角半徑及螺旋圈數(在【L】、【M】狀態下繪圖) 【↓】 矩形、圓角矩形工具 【M】 畫筆工具 【B】 鉛筆、圓滑、抹除工具 【N】 旋轉、轉動工具 【R】 縮放、拉伸工具 【S】 鏡向、傾斜工具 【O】 自由變形工具 【E】 混合、自動勾邊工具 【W】 圖表工具(七種圖表) 【J】 漸變網點工具 【U】 漸變填色工具 【G】 顏色取樣器 【I】 油漆桶工具 【K】 剪刀、餐刀工具 【C】 視圖平移、頁面、尺寸工具 【H】 放大鏡工具 【Z】 默認前景色和背景色 【D】 切換填充和描邊 【X】 標准屏幕模式、帶有菜單欄的全屏模式、全屏模式 【F】 切換為顏色填充 【】 切換為無填充 【/】 臨時使用抓手工具 【空格】 精確進行鏡向、旋轉等操作 選擇相應的工具後按【回車】 復制物體 在【R】、【O】、【V】等狀態下按【Alt】+【拖動】 文件操作 新建圖形文件 【Ctrl】+【N】 打開已有的圖像 【Ctrl】+【O】 關閉當前圖像 【Ctrl】+【W】 保存當前圖像 【Ctrl】+【S】 另存為... 【Ctrl】+【Shift】+【S】 存儲副本 【Ctrl】+【Alt】+【S】 頁面設置 【Ctrl】+【Shift】+【P】 文檔設置 【Ctrl】+【Alt】+【P】 列印 【Ctrl】+【P】 打開“預置”對話框 【Ctrl】+【K】 回復到上次存檔之前的狀態 【F12】 編輯操作 還原前面的操作(步數可在預置中) 【Ctrl】+【Z】 重復操作 【Ctrl】+【Shift】+【Z】 將選取的內容剪切放到剪貼板 【Ctrl】+【X】或【F2】 將選取的內容拷貝放到剪貼板 【Ctrl】+【C】 將剪貼板的內容粘到當前圖形中 【Ctrl】+【V】或【F4】 將剪貼板的內容粘到最前面 【Ctrl】+【F】 將剪貼板的內容粘到最後面 【Ctrl】+【B】 刪除所選對象 【DEL】 選取全部對象 【Ctrl】+【A】 取消選擇 【Ctrl】+【Shift】+【A】 再次轉換 【Ctrl】+【D】 發送到最前面 【Ctrl】+【Shift】+【]】 向前發送 【Ctrl】+【]】 發送到最後面 【Ctrl】+【Shift】+【[】 向後發送 【Ctrl】+【[】 群組所選物體 【Ctrl】+【G】 取消所選物體的群組 【Ctrl】+【Shift】+【G】 鎖定所選的物體 【Ctrl】+【2】 鎖定沒有選擇的物體 【Ctrl】+【Alt】+【Shift】+【2】 全部解除鎖定 【Ctrl】+【Alt】+【2】 隱藏所選物體 【Ctrl】+【3】 隱藏沒有選擇的物體 【Ctrl】+【Alt】+【Shift】+【3】 顯示所有已隱藏的物體 【Ctrl】+【Alt】+【3】 聯接斷開的路徑 【Ctrl】+【J】 對齊路徑點 【Ctrl】+【Alt】+【J】 調合兩個物體 【Ctrl】+【Alt】+【B】 取消調合 【Ctrl】+【Alt】+【Shift】+【B】 調合選項 選【W】後按【回車】 新建一個圖像遮罩 【Ctrl】+【7】 取消圖像遮罩 【Ctrl】+【Alt】+【7】 聯合路徑 【Ctrl】+【8】 取消聯合 【Ctrl】+【Alt】+【8】 圖表類型 選【J】後按【回車】 再次應用最後一次使用的濾鏡 【Ctrl】+【E】 應用最後使用的濾鏡並調節參數 【Ctrl】+【Alt】+【E】 文字處理 文字左對齊或頂對齊 【Ctrl】+【Shift】+【L】 文字中對齊 【Ctrl】+【Shift】+【C】 文字右對齊或底對齊 【Ctrl】+【Shift】+【R】 文字分散對齊 【Ctrl】+【Shift】+【J】 插入一個軟回車 【Shift】+【回車】 精確輸入字距調整值 【Ctrl】+【Alt】+【K】 將字距設置為0 【Ctrl】+【Shift】+【Q】 將字體寬高比還原為1比1 【Ctrl】+【Shift】+【X】 左/右選擇 1 個字元 【Shift】+【←】/【→】 下/上選擇 1 行 【Shift】+【↑】/【↓】 選擇所有字元 【Ctrl】+【A】 選擇從插入點到滑鼠點按點的字元 【Shift】加點按 左/右移動 1 個字元 【←】/【→】 下/上移動 1 行 【↑】/【↓】 左/右移動1個字 【Ctrl】+【←】/【→】 將所選文本的文字大小減小2 點象素 【Ctrl】+【Shift】+【】 將所選文本的文字大小減小10 點象素 【Ctrl】+【Alt】+【Shift】+【】 將行距減小2點象素 【Alt】+【↓】 將行距增大2點象素 【Alt】+【↑】 將基線位移減小2點象素 【Shift】+【Alt】+【↓】 將基線位移增加2點象素 【Shift】+【Alt】+【↑】 將字距微調或字距調整減小20/1000ems 【Alt】+【←】 將字距微調或字距調整增加20/1000ems 【Alt】+【→】 將字距微調或字距調整減小100/1000ems 【Ctrl】+【Alt】+【←】 將字距微調或字距調整增加100/1000ems 【Ctrl】+【Alt】+【→】 游標移到最前面 【HOME】 游標移到最後面 【END】 選擇到最前面 【Shift】+【HOME】 選擇到最後面 【Shift】+【END】 將文字轉換成路徑 【Ctrl】+【Shift】+【O】 視圖操作 將圖像顯示為邊框模式(切換) 【Ctrl】+【Y】 對所選對象生成預覽(在邊框模式中) 【Ctrl】+【Shift】+【Y】 放大視圖 【Ctrl】+【+】 縮小視圖 【Ctrl】+【-】 放大到頁面大小 【Ctrl】+【0】 實際象素顯示 【Ctrl】+【1】 顯示/隱藏所路徑的控制點 【Ctrl】+【H】 隱藏模板 【Ctrl】+【Shift】+【W】 顯示/隱藏標尺 【Ctrl】+【R】 顯示/隱藏參考線 【Ctrl】+【;】 鎖定/解鎖參考線 【Ctrl】+【Alt】+【;】 將所選對象變成參考線 【Ctrl】+【5】 將變成參考線的物體還原 【Ctrl】+【Alt】+【5】 貼緊參考線 【Ctrl】+【Shift】+【;】 顯示/隱藏網格 【Ctrl】+【”】 貼緊網格 【Ctrl】+【Shift】+【”】 捕捉到點 【Ctrl】+【Alt】+【”】 應用敏捷參照 【Ctrl】+【U】 顯示/隱藏“字體”面板 【Ctrl】+【T】 顯示/隱藏“段落”面板 【Ctrl】+【M】 顯示/隱藏“製表”面板 【Ctrl】+【Shift】+【T】 顯示/隱藏“畫筆”面板 【F5】 顯示/隱藏“顏色”面板 【F6】/【Ctrl】+【I】 顯示/隱藏“圖層”面板 【F7】 顯示/隱藏“信息”面板 【F8】 顯示/隱藏“漸變”面板 【F9】 顯示/隱藏“描邊”面板 【F10】 顯示/隱藏“屬性”面板 【F11】 顯示/隱藏所有命令面板 【TAB】 顯示或隱藏工具箱以外的所有調板 【Shift】+【TAB】 選擇最後一次使用過的面板 【Ctrl】+【~】
4. android 如何把正方形圖片顯示圓形
Android應用開發中,很多頭像都要求顯示成圓形的,這就可以使用android的canvas、paint這些類來進行設置圓形,先設置paint的樣式為圓形,然後把你要設置成圓形的圖片重新賦值給paint這個類:canvas.drawBitmap(tempBmp, rect, rect, paint);
核心代碼如下(引用這位前輩:http://blog.sina.com.cn/s/blog_7607703f0101dhlj.html,我增加一些注釋,原來是沒有注釋):
java">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);
}
}
}
}
5. android設置控制項樣式(邊框顏色,圓角)和圖片樣式(圓角)
本文鏈接:https://blog.csdn.net/weixin_37577039/article/details/79090433
```
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorAccent" />
<!-- 這里是設置為四周 也可以單獨設置某個位置為圓角-->
<corners android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"/>
<stroke android:width="1dp" android:color="#000000" />
</shape
```
```
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 邊框顏色值 -->
<item>
<shape>
<solid android:color="#3bbaff" />
</shape>
</item>
<!--這個是按鈕邊框設置為四周 並且寬度為1-->
<item
android:right="1dp"
android:left="1dp"
android:top="1dp"
android:bottom="1dp">
<shape>
<!--這個是背景顏色-->
<solid android:color="#ffffff" />
<!--這個是按鈕中的字體與按鈕內的四周邊距-->
<padding android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
</layer-list>
```
使用:
```android:background="@drawable/button_edge"```
```
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充的顏色 -->
<solid android:color="#FFFFFF" />
<!-- android:radius 弧形的半徑 -->
<!-- 設置按鈕的四個角為弧形 -->
<corners
android:radius="5dip" />
<!--也可單獨設置-->
<!-- <corners -->
<!-- android:topLeftRadius="10dp"-->
<!-- android:topRightRadius="10dp"-->
<!-- android:bottomRightRadius="10dp"-->
<!-- android:bottomLeftRadius="10dp"-->
<!-- /> -->
**設置文字padding**
<!-- padding:Button裡面的文字與Button邊界的間隔 -->
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"
/>
</shape>
```
```
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"/>
</shape>
```
使用:
```
android:background="@drawable/image_circle"
```
```
Glide.with(MainActivity.this).load(croppedUri)
.transform(new GlideRectRound(MainActivity.this,6)).into(headIcon);
```
```
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.Log;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
/**
* Created by SiHao on 2018/3/3.
* Glide 的 圓角 圖片 工具類
*/
public class GlideRectRound extends BitmapTransformation {
private static float radius = 0f;
// 構造方法1 無傳入圓角度數 設置默認值為5
public GlideRectRound(Context context) {
this(context, 5);
}
// 構造方法2 傳入圓角度數
public GlideRectRound(Context context, int dp) {
super(context);
// 設置圓角度數
radius = Resources.getSystem().getDisplayMetrics().density * dp;
}
// 重寫該方法 返回修改後的Bitmap
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return rectRoundCrop(pool,toTransform);
}
@Override
public String getId() {
Log.e("getID",getClass().getName() + Math.round(radius));
return getClass().getName() + Math.round(radius); // 四捨五入
}
private Bitmap rectRoundCrop(BitmapPool pool, Bitmap source){
if (source == null) return null;
Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); // ARGB_4444——代表4x4位ARGB點陣圖,ARGB_8888——代表4x8位ARGB點陣圖
if (result == null) {
result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
// setShader 對圖像進行渲染
// 子類之一 BitmapShader設置Bitmap的變換 TileMode 有CLAMP (取bitmap邊緣的最後一個像素進行擴展),REPEAT(水平地重復整張bitmap)
//MIRROR(和REPEAT類似,但是每次重復的時候,將bitmap進行翻轉)
paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true); // 抗鋸齒
RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rectF, radius, radius, paint);
return result;
}
}
```
圓角:
```
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
/**
* Created by SiHao on 2018/3/3.
* Glide圓形圖片工具類
*/
public class GlideCircleBitmap extends BitmapTransformation{
public GlideCircleBitmap(Context context) {
super(context);
}
// 重寫該方法 返回修改後的Bitmap
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
@Override
public String getId() {
return getClass().getName();
}
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
// 邊長取長寬最小值
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
// TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);// ARGB_4444——代表4x4位ARGB點陣圖,ARGB_8888——代表4x8位ARGB點陣圖
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
// setShader 對圖像進行渲染
// 子類之一 BitmapShader設置Bitmap的變換 TileMode 有CLAMP (取bitmap邊緣的最後一個像素進行擴展),REPEAT(水平地重復整張bitmap)
//MIRROR(和REPEAT類似,但是每次重復的時候,將bitmap進行翻轉)
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);// 抗鋸齒
// 半徑取 size的一半
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return result;
}
}
```
```
URL url = new URL(String類型的字元串); //將String類型的字元串轉換為URL格式
holder.UserImage.setImageBitmap(BitmapFactory.decodeStream(url.openStream()));
```
```
//得到資源文件的BitMap
Bitmap image= BitmapFactory.decodeResource(getResources(),R.drawable.dog);
//創建RoundedBitmapDrawable對象
RoundedBitmapDrawable roundImg =RoundedBitmapDrawableFactory.create(getResources(),image);
//抗鋸齒
roundImg.setAntiAlias(true);
//設置圓角半徑
roundImg.setCornerRadius(30);
//設置顯示圖片
imageView.setImageDrawable(roundImg);
```
```
//如果是圓的時候,我們應該把bitmap圖片進行剪切成正方形, 然後再設置圓角半徑為正方形邊長的一半即可
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
Bitmap bitmap = null;
//將長方形圖片裁剪成正方形圖片
if (image.getWidth() == image.getHeight()) {
bitmap = Bitmap.createBitmap(image, image.getWidth() / 2 - image.getHeight() / 2, 0, image.getHeight(), image.getHeight());
} else {
bitmap = Bitmap.createBitmap(image, 0, image.getHeight() / 2 - image.getWidth() / 2, image.getWidth(), image.getWidth());
}
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
//圓角半徑為正方形邊長的一半
roundedBitmapDrawable.setCornerRadius(bitmap.getWidth() / 2);
//抗鋸齒
roundedBitmapDrawable.setAntiAlias(true);
imageView.setImageDrawable(roundedBitmapDrawable);
```