當前位置:首頁 » 安卓系統 » android圖片填充顏色

android圖片填充顏色

發布時間: 2022-11-22 12:34:09

① android 里用shape畫圓,怎麼填充顏色

背景色那裡改一下就行了

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:useLevel="false">
<!--背景色-->
<solid android:color="@color/redbg"/>
<!-- 邊框色 -->
<stroke android:width="1dp"
android:color="#ee5859"/>

<size android:width="95dp"
android:height="95dp"/>

</shape>

② android 里用shape畫圓,怎麼填充顏色

增加一個填充就好了
<!-- 填充 -->
<solid
android:color="@android:color/white"/><!-- 填充的顏色 -->

③ 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 里用shape畫圓,怎麼填充顏色

<shape>節點下的<solid>節點是用來設置填充顏色的。
使用方法只需要添加代碼<solid android:color= "填充色" />
例如<solid android:color= "@color/red" />或者<solid android:color= "#ffff00" />

⑤ android 里用shape畫圓,怎麼填充顏色

用圓形選區(M)按住ALT+SHIFT鍵拖出一個圓形。然後將前景色調至圖片的顏色(可以用吸管吸色)。填充顏色(ALT+

⑥ android 里用shape畫圓,怎麼填充顏色

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 控制項裡面的顏色,如果是EditText的話裡面設置#ffffff 或者自己喜歡的顏色-->
<solid android:color="@color/new_blue"/>
<!-- 圓角度-->
<corners android:radius="2dp"/>
<!-- 控制項外面邊框的顏色和寬度 如果選擇不要邊框的去掉這個就是了-->
<stroke android:width="1px" android:color="@color/blue"/>
</shape>

⑦ android 里用shape畫圓,怎麼填充顏色

android 用shape畫圓,使用<solid android:color="#f00" />(#f00表示填充的顏色),該屬性來填充顏色,在Android studio按照如下步驟創建圓點並添加顏色屬性。

1、在使用Android studio創建的Android項目中,在drawable中創建一個空白文件,如下圖:

⑧ android 里用shape畫圓,怎麼填充顏色

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="

>
<stroke android:width="1dp" android:color="#ff0000" /> <!--描邊-->
<solid android:color="#ff0000" /><!--填充-->
</shape>

⑨ android 里用shape畫圓,怎麼填充顏色

Android裡面使用shape設置控制項的外形,例如一些圓角、填充的背景顏色、以及一些漸變的效果等,所以設置填充顏色,可通過設置shape.xml文件里的如下屬性:

<solidandroid:color="@color/common_red"/>

將shape文件放到android的button、textview組件上,就可以有填充背景顏色的效果,完整的代碼如下:

1.shape.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:useLevel="false">
<solidandroid:color="@color/common_red"/>
<padding
android:left="2dp"
android:top="1dp"
android:right="2dp"
android:bottom="1dp"/>
<solid
android:color="@color/common_red"/>
<stroke
android:width="1dp"
android:color="@android:color/white"/>
<sizeandroid:width="15dp"
android:height="15dp"/>
</shape>


2.把以上代碼添加到drawable裡面、通過background引用就可以了

<TextView
android:id="@id/message_category_unread_count"
style="@style/comm_text_style_14_aaaaaa"
android:layout_marginLeft="70dp"
android:layout_marginTop="5dp"
android:background="@drawable/shape"
android:gravity="center"
android:textSize="@dimen/text_size_comment_20"
android:text="7"
android:textColor="@android:color/white"/>

效果如下圖:

⑩ android 里用shape畫圓,怎麼填充顏色

為你其中提供一種解決方案:
android;msg_new_counter"22"
android;
android;20dp"
android;>.com/apk/res/android"0dp" android;
android;
android;
android;utf-8".android;/://schemas;TextView
android; /:width="shape xmlns;top|center_horizontal".com/apk/res/android" target="_blank">http.0":background="3dp":text=":layout_height="bold" android;false"
android:textColor="ring"solid android;>
android;bg_have_msg".android://schemas;wrap_content":layout_width=";2":layout_marginTop="8sp":shape="stroke android; encoding="1;
android;3dp"
android;
<:id="
android;
<

2;#ffffff":useLevel="wrap_content"?xml version=":innerRadiusRatio=":textSize="#ff0000">@+id/1"
<
android;
android:color=":layout_marginLeft="#ff0000"
android:thicknessRatio=":color=".資源文件

<?>:textStyle="/ /:innerRadius=":padding=":android=">
<

在android studio的布局預覽里顯示正常(紅底白字的圓):thickness="
android布局文件

<1dp"

熱點內容
伺服器怎麼連接到電腦顯示屏上 發布:2024-05-21 02:38:21 瀏覽:285
織夢安裝資料庫連接失敗 發布:2024-05-21 02:37:45 瀏覽:258
python編程入門經典pdf 發布:2024-05-21 02:31:45 瀏覽:6
arm編譯添加驅動 發布:2024-05-21 02:02:28 瀏覽:476
安卓設置頁面是怎麼 發布:2024-05-21 01:32:51 瀏覽:521
學生成績管理系統資料庫設計 發布:2024-05-21 01:14:41 瀏覽:43
我的世界什麼指令直接出現伺服器 發布:2024-05-21 01:10:00 瀏覽:397
星等演算法 發布:2024-05-21 00:53:06 瀏覽:509
李興華的java視頻 發布:2024-05-21 00:49:55 瀏覽:605
資料庫4種索引類型 發布:2024-05-21 00:47:29 瀏覽:241