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

androidshape圓角

發布時間: 2024-06-21 00:29:52

⑴ 如何給imageview設置圓角

android設置imageview圓角主要使用的是shape.xml文件,對四個角的角度進行設置,只要再imageview的background的屬性上加上shape文件即可達到效果,示例如下:
shapeyuanjiao3.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<!-- 填充的顏色 -->
<solid android:color="#0000FF" />
<!-- 設置按鈕的四個角為弧形 -->
<!-- android:radius 弧形的半徑 -->
<corners android:topLeftRadius="@dimen/RoundedAmplitude" android:topRightRadius="@dimen/RoundedAmplitude"/>

<gradient
android:angle="270"
android:centerColor="#0000FF"
android:endColor="#0000FF"
android:startColor="#0000FF" />

</shape>
imageview、textview等的android:background="@drawable/shapeyuanjiao3"屬性,設置尺寸、圓角(可以定製單獨顯示哪個角需要圓角)。可以參考我的csdn博客的這篇文章:http://blog.csdn.net/nihaoqiulinhe/article/details/46833819

⑵ Android如何設置圓角按鈕

可以通過shape設置圓角
<!-- 設置圓角 -->
<corners android:radius="2dp" >
</corners>
擴展:
<?xml version="1.0" encoding="utf-8"?>
<!-- shape如果不聲明形狀默認是正方形 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 設置填充色 -->

<solid android:color="#4285f4" >
</solid>
<!-- 設置邊框的顏色和寬度 -->
<stroke
android:width="1dp"
android:color="#4285f4" >
</stroke>
</shape>
通過selector設置點擊效果

button_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 這個是用於控制按鈕組背景的文件 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- **點擊時效果**********背景引用的資源*************************是否獲得焦點*********************是否按下******* -->
<item android:drawable="@drawable/button_p" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/button_p" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/button_p" android:state_focused="false" android:state_pressed="true"/>

<!-- **************沒有任何操作時顯示的背景************** -->
<item android:drawable="@drawable/button_n"></item>
</selector>
在xml文件中設置button的background屬性。
android:background="@drawable/button_bg"

⑶ android 圓角樣式shape 的shape屬性什麼意思

shape官方給出了很多屬性的解釋,如下:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
><!-- 其中rectagle表示矩形,oval表示橢圓,line表示水平直線,ring表示環形 -->
<!-- 節點屬性介紹如下 -->
<corners />
<!-- 節點1:corners (圓角)
android:radius 圓角的半徑 值越大角越圓
android:topLeftRadius 左上圓角半徑
android:topRightRadius 右上圓角半徑
android:bottomLeftRadius 左下圓角半徑
android:bottomRightRadius 右下圓角半徑
-->

<gradient />
<!-- 節點2: gradient (背景顏色漸變)
android:startColor 起始顏色
android:centerColor 中間顏色
android:endColor 末尾顏色
android:angle 漸變角度,必須為45的整數倍。
android:type 漸變模式 默認是linear(線性漸變)radial(環形漸變)
android:centerX X坐標
android:centerY Y坐標
android:gradientRadius radial(環形漸變)時需指定半徑
-->

<padding />
<!-- 節點3: padding (定義內容離邊界的距離)
android:left 左部邊距
android:top 頂部邊距
android:right 右部邊距
android:bottom 底部邊距
-->

<size />
<!-- 節點4:size (大小)
android:width 指定寬度
android:height 指定高度
-->

<solid />
<!-- 節點5:solid (填充)
android:color 指定填充的顏色
-->

<stroke />
<!-- 節點6:stroke (描邊)
android:width 描邊的寬度
android:color 描邊的顏色
把描邊弄成虛線的形式"- - -":
android:dashWidth 表示'-'這樣一個橫線的寬度
android:dashGap 表示之間隔開的距離
-->

</shape></span>

⑷ android代碼的形式讓button變成圓角 透明,如圖

步驟如下:
------------------------------------------------------------------------
1. 先在res/drawable中定義一個shape.xml文件,具體的顏色你可以自己調
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<!-- 填充的顏色:這里設置背景透明 -->
<solid android:color="@android:color/transparent" />
<!-- 邊框的顏色 :不能和窗口背景色一樣-->
<stroke
android:width="3dp"
android:color="#ffffff" />
<!-- 設置按鈕的四個角為弧形 -->
<!-- android:radius 弧形的半徑 -->
<corners android:radius="5dip" />

<!-- padding:Button裡面的文字與Button邊界的間隔 -->
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
------------------------------------------------------------------------
2. 在你的Activity的xml(比如activity_main.xml)中定義按鈕
<Button
android:id="@+id/roundButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape"
android:text=" 圓角按鈕 " />

⑸ Android studio圓角矩形的布局如何設計

你可以使用shape定義一個圓角矩形,並將其作為布局的背景即可。
圓角矩形的shape代碼如下:
//定義四個圓角 文件名shape_round_corner
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ff00" />
<corners android:topLeftRadius="12dp"
android:topRightRadius="12dp"
android:bottomRightRadius="12dp"
android:bottomLeftRadius="12dp"/>
<stroke android:width="1dp" android:color="#ff0" />
</shape>
設置背景代碼如下:
<LinearLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:alpha="0.8"
android:background="@drawable/shape_round_corner">
</LinearLayout>

⑹ android中我shape里寫了邊框顏色、圓角,但調用過去沒實現。。。

你是說View占滿了整個屏幕嗎?這樣的話就只能在layout中寫一個布局文件,將View以一種組件的方式聲明到.xml文件中,然後你可以在它的上下中聲明其他的button或者textview或者ImageButton等。

⑺ Android studio中怎麼將方形按鈕設置成圓角以及漸變效果

一、在 studio中res 包下的drawable中建立一個shape的文件,系統會給你一個默認的方形然後你就可以開始設置你需要的效果。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:Android="http://schemas.android.com/apk/res/android">

<!--填充背景色 -->
<solid android:color="#ffff00" /> //solid 指的是背景顏色的設置

<!--描邊 他需要 2個參數,顏色 第二參數 寬度 -->
<stroke
android:width="2dp" //width 指的是邊框的寬度
android:color="#ff00ff" /> //color指的是邊框的顏色

<!-- 設置弧度 Radius設置所有角的弧度 --> //radius 指的就是角的弧度
<corners //方形存在四個角,我們可以同時設置四個角,也可以分開設置
android:bottomLeftRadius="20dp" //左下角
android:bottomRightRadius="20dp" //右下角
android:topLeftRadius="20dp" //左上角
android:topRightRadius="20dp" /> //右上角

//同時設置:<corners
android:Radius="20dp" /> //四個角同時設置

<!-- 漸變色 startColor 啟示顏色 endColor最終顏色 type類型(如果類型為radial(徑向漸變,一個圓心以半徑的方式漸變),必須加上 android:gradientRadius="**")-->
<gradient
android:startColor="#ff0000" //開始顏色
android:endColor="#ffffff" //結束顏色
android:type="linear" /> //水平漸變 從左到右 type 指的是漸變的模式

<!--內容與邊框的間距-->
<padding
android:bottom="15dp"
android:left="15dp"
android:right="15dp"
android:top="15dp" />

<!--如果設置了漸變色 那麼背景顏色將不顯示 -->

</shape>
<gradient>
shape的顏色漸變屬性
attributes:

android:angle
Integer,代表漸變顏色的角度, 0 is left to right, 90 is bottom to top. 必須是45的整數倍.
默認是 0.該屬性只有在type=linear情況下起作用,默認的type為linear。
默認情況下,從左到右:

xml代碼:<gradient
android:startColor="#000000"
android:endColor="#ffffff"
/>

angle=270,從上到下 :

xml代碼:<gradient
android:startColor="#000000"
android:endColor="#ffffff"
android:angle="270"
/>
android:startColor
Color. 顏色漸變的開始顏色,如angle=270中的 android:startColor="#000000"
android:endColor
Color. 顏色漸變的結束顏色,如angle=270中的 android:endColor="#ffffff"
android:centerColor
Color. 顏色漸變的中間顏色,主要用於多彩。

<gradient
android:startColor="#000000"
android:endColor="#ffffff"
android:centerColor="#ff0000"
/>
android:centerX
Float.(0 - 1.0) 相對X的漸變位置。
android:centerY
Float.(0 - 1.0) 相對Y的漸變位置。
這兩個屬性只有在type不為linear情況下起作用。
android:gradientRadius
Float. 漸變顏色的半徑,單位應該是像素點. 需要 android:type="radial".
如果android:type="radial",沒有設置android:gradientRadius,將會報錯,error inflating class.

xml代碼:
<gradient
android:startColor="#ff0000"
android:endColor="#ffffff"
android:centerX="0.5"
android:centerY="0.5"
android:gradientRadius="30"
android:type="radial"
/>
加入Android:centerColor屬性

<gradient
android:startColor="#ff0000"
android:endColor="#ffffff"
android:centerColor="#000000"
android:centerX="0.5"
android:centerY="0.5"
android:gradientRadius="30"
android:type="radial"
/>
android:type
Value Description
"linear" 線性漸變.可以理解為 y=kx+b.
"radial" A radial gradient.圓形漸變,起始顏色從cenralX,centralY點開始。
"sweep" A sweeping line gradient.

centerX="0.2" centerX="0.2"

xml代碼:
<gradient
android:startColor="#ff0000"
android:endColor="#ffffff"
android:centerX="0.2"
android:centerX="0.2"
android:gradientRadius="30"
android:type="radial"
/>
type="sweep":

xml代碼:
<gradient
android:startColor="#ff0000"
android:endColor="#ffffff"
android:centerX="0.5"
android:centerY="0.5"
android:type="sweep"
/>

⑻ 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如何實現按下按鈕顏色會變

需要定義兩個不同顏色的圓角xml布局,selector的drawable引用的就是圓角的xml,最後,布局調用的是selector。

⑽ 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" /> //虛線的間隔

熱點內容
去哪裡找自己的支付密碼 發布:2024-10-25 14:46:18 瀏覽:417
生產文件夾 發布:2024-10-25 14:46:14 瀏覽:705
windows搭建ftp伺服器埠修改 發布:2024-10-25 14:46:11 瀏覽:322
勞拉與馬ftp 發布:2024-10-25 00:21:16 瀏覽:359
奪寶網站源碼 發布:2024-10-25 00:19:02 瀏覽:454
編程文本編輯器 發布:2024-10-25 00:09:28 瀏覽:972
編程徐帥 發布:2024-10-25 00:03:25 瀏覽:307
手機安卓模擬器如何打開文件 發布:2024-10-25 00:02:55 瀏覽:722
pythonday 發布:2024-10-24 23:55:47 瀏覽:425
g編譯c文件 發布:2024-10-24 23:55:03 瀏覽:294