當前位置:首頁 » 安卓系統 » 畫圓角android

畫圓角android

發布時間: 2022-10-28 18:52:18

㈠ 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中使用CardView實現圓角對話框

前言:隨著用戶體驗的不斷的加深,良好的UI視覺效果也必不可少,以前方方正正的對話框樣式在APP已不復存在,取而代之的是帶有圓角效果的Dialog,之前設置對畫框的圓角效果都是通過drawable/shape屬性來完成,隨著Google API的不斷更新,API 21(Android 5.0)添加了新的控制項CardView,這使得圓角的實現更加方便快捷。

效果圖:

導入CardView依賴(API 21新控制項)

1.cardCornerRadius屬性:設置圓角的弧度大小,這里設置的為10dp

2.CardView還有padding、cardUseCompatPadding(內邊距)、background等屬性

3.CardView繼承自FrameLayout,使用時可以重新嵌套布局

使用的是V7包的AlertDialog實現的,當然也可以使用Dialog實現。

總結:CardView實現對話框的圓角效果更加的方便,不用編寫shape屬性設置背景,當標題欄需要背景色時,也無需考慮設置標題欄的shape背景(不使用CardView時,如果不使用shape設置背景色,會導致左上和右上不會變成圓角)。

㈢ Android如何設置圓角圖片

可以使用自定義控制項顯示圓角效果,或者使用style
設置shape,最直接的就是直接使用圓角圖片,參考資料:http://www.it399.com/imageRound,望採納,謝謝。

㈣ android怎麼畫圓角的矩形

如果你是在自定義view的onDraw方法中:

java">RectFrectF=newRectF(100,100,500,500);//先畫一個矩形
Paintpaint=newPaint(Paint.ANTI_ALIAS_FLAG);//創建畫筆
paint.setColor(R.color.colorAccent);//添加畫筆顏色
canvas.drawRoundRect(rectF,30,30,paint);//根據提供的矩形為四個角畫弧線,(其中的數字:第一個表示X軸方向大小,第二個Y軸方向大小。可以改成其他的,你可以自己體驗),最後添加畫筆。

如果你是在布局中直接添加,樓上已經做出方法,我就不舉例了。

㈤ android中如何將圖片變為圓角

方法一:使用框架CircleImageView,這是直接使用圓形/圓角的ImageView

方法二:使用圖片載入框架Glide,這是使用正常的ImageView,在載入圖片的時候進行圓形/圓角處理。

GlideApp.with(this).load("http://123.jpg").circleCrop().into(imageView)
GlideApp.with(this).load("http://123.jpg").transform(newGlideRoundTransform(context,10)).into(imageView)

㈥ android 怎麼畫一個圓角矩形

有兩種方法設置:

一:定義一個xml布局文件,分別設置四個角
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 背景色 -->
<solid android:color="#FEBB66"/>
<!-- 邊框色 -->
<stroke android:width="0.3dip" android:color="#ffffff" />
<!-- 邊角:設置圓角大小 -->
<corners android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"/>
</shape>
二:把上面設置邊角的標簽屬性改成如下
<!-- 邊角:設置圓角大小 -->
<corners android:radius="5dp"/>

以上兩種效果是一樣的。然後引用這個布局文件就好

㈦ Android如何設置圓角圖片

可以使用自定義控制項顯示圓角效果,或者使用style 設置shape,最直接的就是直接使用圓角圖片,參考資料:http://www.it399.com/imageRound,望採納,謝謝。

㈧ Android設置圖片圓角的方法

Android中經常會遇到對圖片進行二次處理,例如加圓角,或者顯示圓形圖片

通過第三方框架Glide實現圖片顯示有圓角,有三種寫法如下:

1.1、第一種實現:

1.2、第二種實現:

1.3、第三種實現:

自定義ImageView:

對圖片進行處理,此方法還可以加邊框

實現圓形和邊框:

以上就是本文的全部內容,希望對大家的學習有所幫助!

㈨ Android如何畫圓角矩形

建立 rect_gray.xml文件放在drawable文件夾下面。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充顏色 -->
<solid android:color="#FFFFFF"></solid>

<!-- 線的寬度,顏色灰色 -->
<stroke android:width="1dp" android:color="#D5D5D5"></stroke>

<!-- 矩形的圓角半徑 -->
<corners android:radius="0dp" />

</shape>

然後在布局的xml裡面:
作為ImageView或者Linearlayout等作為背景源就可以了。
<LinearLayout
android:id="@+id/activity_myhezu_wantchuzu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/myhezu_dottedline_rect_green"
android:orientation="horizontal" >

㈩ android 如何重寫imageview 讓圖片有圓角效果

android 自定義圓角ImageView以及鋸齒的處理

看到很多人開發過程中要使用圓角圖片時,解決方法有:


1.重新繪制一張圖片


2.通過布局來配置


3.通過重寫View來實現


其中1,2在這里就不講了,重點講講方法三的實現。



實現一:通過截取畫布一個圓形區域與圖片的相交部分進行繪制,缺點:鋸齒明顯,設置Paint,Canvas抗鋸齒無效。

package com.open.circleimageview.widget;


import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.PaintFlagsDrawFilter;

import android.graphics.Path;

import android.graphics.Rect;

import android.graphics.Region;

import android.util.AttributeSet;

import android.view.View;


public class CircleImageViewA extends View {


public CircleImageViewA(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}


public CircleImageViewA(Context context, AttributeSet attrs) {

super(context, attrs);

}


public CircleImageViewA(Context context) {

super(context);

}


private Bitmap bitmap;

private Rect bitmapRect=new Rect();

private PaintFlagsDrawFilter pdf=new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);

private Paint paint = new Paint();

{

paint.setStyle(Paint.Style.STROKE);

paint.setFlags(Paint.ANTI_ALIAS_FLAG);

paint.setAntiAlias(true);// 設置畫筆的鋸齒效果。 true是去除,大家一看效果就明白了

}

private Path mPath=new Path();

public void setImageBitmap(Bitmap bitmap)

{

this.bitmap=bitmap;

}

@Override

protected void onDraw(Canvas canvas) {


if(null==bitmap)

{

return;

}

bitmapRect.set(0, 0, getWidth(), getHeight());

canvas.save();

canvas.setDrawFilter(pdf);

mPath.reset();

canvas.clipPath(mPath); // makes the clip empty

mPath.addCircle(getWidth()/2, getWidth()/2, getHeight()/2, Path.Direction.CCW);

canvas.clipPath(mPath, Region.Op.REPLACE);

canvas.drawBitmap(bitmap, null, bitmapRect, paint);

canvas.restore();

}

}


實現二:通過PorterDuffXfermode 方式(注意要設置硬體加速,否則部分機子無效),優點:鋸齒基本沒有

package com.open.circleimageview.widget;


import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.PaintFlagsDrawFilter;

import android.graphics.PorterDuff;

import android.graphics.PorterDuffXfermode;

import android.graphics.Rect;

import android.graphics.RectF;

import android.util.AttributeSet;

import android.view.View;


public class CircleImageViewB extends View {


public CircleImageViewB(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init();

}


public CircleImageViewB(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}


public CircleImageViewB(Context context) {

super(context);

init();

}


private Bitmap bitmap;

private Rect bitmapRect=new Rect();

private PaintFlagsDrawFilter pdf=new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);

private Paint paint = new Paint();

{

paint.setStyle(Paint.Style.STROKE);

paint.setFlags(Paint.ANTI_ALIAS_FLAG);

paint.setAntiAlias(true);// 設置畫筆的鋸齒效果。 true是去除,大家一看效果就明白了

}

private Bitmap mDstB=null;

private PorterDuffXfermode xfermode=new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY);

private void init()

{

try {

if(android.os.Build.VERSION.SDK_INT>=11)

{

setLayerType(LAYER_TYPE_SOFTWARE, null);

}

} catch (Exception e) {

e.printStackTrace();

}

}

public void setImageBitmap(Bitmap bitmap)

{

this.bitmap=bitmap;

}


private Bitmap makeDst(int w, int h)

{

Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

Canvas c = new Canvas(bm);

Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);

p.setColor(Color.parseColor("#ffffffff"));

c.drawOval(new RectF(0, 0, w, h), p);

return bm;

}

@Override

protected void onDraw(Canvas canvas) {


if(null==bitmap)

{

return;

}

if(null==mDstB)

{

mDstB=makeDst(getWidth(), getHeight());

}


bitmapRect.set(0, 0, getWidth(), getHeight());

canvas.save();

canvas.setDrawFilter(pdf);

canvas.drawBitmap(mDstB, 0, 0, paint);

paint.setXfermode(xfermode);

canvas.drawBitmap(bitmap, null, bitmapRect, paint);

paint.setXfermode(null);

canvas.restore();

}

}


熱點內容
sql表添加記錄 發布:2025-02-01 11:22:08 瀏覽:864
word編輯加密 發布:2025-02-01 11:18:53 瀏覽:571
php變數文本 發布:2025-02-01 11:10:46 瀏覽:426
音悅台上傳mv 發布:2025-02-01 11:05:02 瀏覽:516
微信如何設置訪問限制 發布:2025-02-01 10:43:06 瀏覽:335
b站緩存視頻下架還有嗎 發布:2025-02-01 10:37:52 瀏覽:940
e卡通初始密碼是多少 發布:2025-02-01 10:31:55 瀏覽:127
phppost上傳文件 發布:2025-02-01 10:26:42 瀏覽:105
伺服器不能寫入ip地址 發布:2025-02-01 10:18:56 瀏覽:129
青驕如何重置賬號密碼 發布:2025-02-01 09:57:51 瀏覽:520