㈠ 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);
```
㈡ Android 怎麼在點擊按鈕後更改背景
只需要給Button配置一個Selector背景選擇器即可實現。
1.創建mylist_view.xml文件
首先在res目錄下新建drawable文件夾,再在新建的drawable文件夾中新建mylist_view.xml,其目錄結構為:res/drawable/mylist_view.xml。
2.根據具體需求編輯mylist_view.xml文件
新建mylist_view.xml文件後,在沒有添加任何屬性時其內部代碼結構為:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
</selector>
3.下面就可以根據項目需求,在其內部定義為自己想要的樣式了,主要屬性如下:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 默認時的背景圖片-->
<item android:drawable="@drawable/pic1" />
<!-- 沒有焦點時的背景圖片 -->
<item android:state_window_focused="false" android:drawable="@drawable/pic1" />
<!-- 非觸摸模式下獲得焦點並單擊時的背景圖片 -->
<item android:state_focused="true" android:state_pressed="true" android:drawable= "@drawable/pic2" />
<!-- 觸摸模式下單擊時的背景圖片-->
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/pic3" />
<!--選中時的圖片背景-->
<item android:state_selected="true" android:drawable="@drawable/pic4" />
<!--獲得焦點時的圖片背景-->
<item android:state_focused="true" android:drawable="@drawable/pic5" />
</selector>
4.引用mylist_view.xml文件
android:background="@drawable/mylist_view"