當前位置:編程學習大全網 - 源碼下載 - android設置控件樣式(邊框顏色,圓角)和圖片樣式(圓角)

android設置控件樣式(邊框顏色,圓角)和圖片樣式(圓角)

本文鏈接:/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="/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="/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="/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);

```

  • 上一篇:新建了壹個動易網站,現在要將老網站(非動易)的數據導入到新網站裏怎麽做
  • 下一篇:王者榮耀代練通的首圖截圖和完單截圖怎麽交?
  • copyright 2024編程學習大全網