導航:首頁 > 操作系統 > androidgradient漸變

androidgradient漸變

發布時間:2022-10-06 09:53:34

『壹』 ios 怎麼做到安卓的lineargradient效果

android 使用LinearGradient進行字體漸變的效果,如下圖顯示:

就像上面的顯示效果一樣一束白光閃過,這種效果主要還是使用了LinearGradient類來進行的

LinearGradient也稱作線性渲染,LinearGradient的作用是實現某一區域內顏色的線性漸變效果

它有兩個構造函數
代碼如下 復制代碼
public LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, Shader.TileMode tile)

其中,參數x0表示漸變的起始點x坐標;參數y0表示漸變的起始點y坐標;參數x1表示漸變的終點x坐標;參數y1表示漸變的終點y坐標;color0表示漸變開始顏色;color1表示漸變結束顏色;參數tile表示平鋪方式。

Shader.TileMode有3種參數可供選擇,分別為CLAMP、REPEAT和MIRROR:

CLAMP的作用是如果渲染器超出原始邊界范圍,則會復制邊緣顏色對超出范圍的區域進行著色

REPEAT的作用是在橫向和縱向上以平鋪的形式重復渲染點陣圖

MIRROR的作用是在橫向和縱向上以鏡像的方式重復渲染點陣圖

public LinearGradient (float x0, float y0, float x1, float y1, int[] colors, float[] positions, Shader.TileMode tile);

其中,參數x0表示漸變的起始點x坐標;參數y0表示漸變的起始點y坐標;參數x1表示漸變的終點x坐標;參數y1表示漸變的終點y坐標;參數colors表示漸變的顏色數組;參數positions用來指定顏色數組的相對位置;參數tile表示平鋪方式。通常,參數positions設為null,表示顏色數組以斜坡線的形式均勻分布。

下面這段代碼是直接從git上面的項目拷貝下來的
代碼如下 復制代碼

package com.example.shimmer;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.TextView;

public class MyTextView extends TextView {

private LinearGradient mLinearGradient;
private Matrix mGradientMatrix;
private Paint mPaint;
private int mViewWidth = 0;
private int mTranslate = 0;

private boolean mAnimating = true;

public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (mViewWidth == 0) {
mViewWidth = getMeasuredWidth();
if (mViewWidth > 0) {
mPaint = getPaint();
mLinearGradient = new LinearGradient(-mViewWidth, 0, 0, 0,
new int[] { 0x33ffffff, 0xffffffff, 0x33ffffff },
new float[] { 0, 0.5f, 1 }, Shader.TileMode.CLAMP);
mPaint.setShader(mLinearGradient);
mGradientMatrix = new Matrix();
}
}
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mAnimating && mGradientMatrix != null) {
mTranslate += mViewWidth / 10;
if (mTranslate > 2 * mViewWidth) {
mTranslate = -mViewWidth;
}
mGradientMatrix.setTranslate(mTranslate, 0);
mLinearGradient.setLocalMatrix(mGradientMatrix);
postInvalidateDelayed(50);
}
}

}

這段代碼主要是分兩步:一個是在onSizeChanged()即大小發生改變的時候,另外一個是onDraw()主要是用來做動畫的效果的,

『貳』 android 怎麼自定義顏色漸變

1.在res/drawable/里新建XML文件(background_color.xml)
內容:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#000000"
android:endColor="#FFFFFF"
android:angle="90"
/>

</shape>
備:angle(角度)的值只可為:45 90 135 180等45的倍數

2.在res/layout里使用時:
顏色設置段:Android:src="@drawable/background_color

『叄』 android gradient與solid是不是只能同時一個生效

android顏色漸變的分類有: LinearGradient線性漸變 RadialGradient鏡像漸變 SweepGradient角度漸變 一、LinearGradient線性漸變顧名思義,是只顏色在一個直線方向上

『肆』 android 如何實現背景圖片漸變切換

解決方案1:
其他的和線性漸變相同。為了演 示圖像漸變效果。

一, float radius, float x1。

SweepGradient(float cx。

LinearGradient(float x0,所以建議使用下面的重載方法,他們的基類為android、RadialGradient鏡像漸變
有了上面的基礎. If positions is NULL,代碼如下,他們的不同之處為參數中第一種方法可以用顏色數組, int color1, float radius;/,然後通過載入的顏色來平均的漸變渲染, the drawing may proce unexpected results, int[] colors.RED,p).TileMode tile)

使用實例如下.TileMode, float[] positions; /, int color0,最後參數為平鋪方式, Shader,100, float y0. If the values are not monotonic.graphics, float y1,這里設置為鏡像
剛才已經講到Gradient是基於Shader類,前兩個參數為中心點.TileMode tile)

LinearGradient(float x0.drawCicle(0;
canvas.Shader,參數三和四分辨對應漸變終點.TileMode tile)

三, int color0,所以我們通過Paint的setShader方法來設置這個漸變,200,則漸變將會逐一處理。
二, float[] positions,徑向漸變第三個參數是半徑, int[] colors在android, beginning with 0 and ending with 1,本方法一般為NULL即可.、SweepGradient角度漸變
對於一些3D立體效果的漸變可以嘗試用角度漸變來完成一個圓錐形, float cy. The relative position of each corresponding color in the colors array.setShader(lg),和位置來實現更細膩的過渡效果, 比如顏 色采樣int[] colors數組中存放20種顏色, float x1,Shader。

RadialGradient(float x, float[] positions) /。而第二種方法參數僅為起初顏色color0和最終顏色color1, Shader。和上面參數唯一不同的是, then the colors are automatically spaced evenly,0, float y,了解這些對打好Android游戲開發的基礎很有好處;對於最後一個參數SDK上的描述為May be NULL.graphics中提供了有關Gradient字樣的類, Shader。

SweepGradient(float cx;參數3為畫圓的半徑;
LinearGradient lg=new LinearGradient(0,希望大家對圖像特效處理有了一定的認識,0,參數二為y軸位置,我們一起來了解下徑向漸變;/,類型為float型,相對來說比上面更簡單,Color; /, int color1)

到此,100,Color.TileMode tile)

RadialGradient(float x.MIRROR).BLUE, float y0, int color0,例如LinearGradient線性漸變:

p, int color1、LinearGradient線性漸變
在android平台中提供了兩種重載方式來實例化該類分別為,下面給出一個簡單的實例, int[] colors, float cy, Shader, float y1;參數一為漸變起初點坐標x位置:

Paint p=new Paint(), float y.0、 RadialGradient徑向漸變和SweepGradient角度漸變三種。

『伍』 android怎麼用paint實現圖像的漸變出現

在android.graphics中提供了有關Gradient字樣的類,例如LinearGradient線性漸變、RadialGradient徑向漸變和SweepGradient角度漸變三種,他們的基類為android.graphics.Shader。為了演示圖像漸變效果,下面給出一個簡單的實例。一、LinearGradient線性漸變在android平台中提供了兩種重載方式來實例化該類分別為,他們的不同之處為參數中第一種方法可以用顏色數組,和位置來實現更細膩的過渡效果,比如顏色采樣int[]colors數組中存放20種顏色,則漸變將會逐一處理。而第二種方法參數僅為起初顏色color0和最終顏色color1。LinearGradient(floatx0,floaty0,floatx1,floaty1,int[]colors,float[]positions,Shader.TileModetile)LinearGradient(floatx0,floaty0,floatx1,floaty1,intcolor0,intcolor1,Shader.TileModetile)使用實例如下:Paintp=newPaint();LinearGradientlg=newLinearGradient(0,0,100,100,Color.RED,Color.BLUE,Shader.TileMode.MIRROR);//參數一為漸變起初點坐標x位置,參數二為y軸位置,參數三和四分辨對應漸變終點,最後參數為平鋪方式,這里設置為鏡像剛才已經講到Gradient是基於Shader類,所以我們通過Paint的setShader方法來設置這個漸變,代碼如下:p.setShader(lg);canvas.drawCicle(0,0,200,p);//參數3為畫圓的半徑,類型為float型。二、RadialGradient鏡像漸變有了上面的基礎,我們一起來了解下徑向漸變。和上面參數唯一不同的是,徑向漸變第三個參數是半徑,其他的和線性漸變相同。RadialGradient(floatx,floaty,floatradius,int[]colors,float[]positions,Shader.TileModetile)RadialGradient(floatx,floaty,floatradius,intcolor0,intcolor1,Shader.TileModetile)三、SweepGradient角度漸變對於一些3D立體效果的漸變可以嘗試用角度漸變來完成一個圓錐形,相對來說比上面更簡單,前兩個參數為中心點,然後通過載入的顏色來平均的漸變渲染。SweepGradient(floatcx,floatcy,int[]colors,float[]positions)//對於最後一個參數SDK上的描述為MaybeNULL.,beginningwith0andendingwith1.0.Ifthevaluesarenotmonotonic,.IfpositionsisNULL,.,所以建議使用下面的重載方法,本方法一般為NULL即可。SweepGradient(floatcx,floatcy,intcolor0,intcolor1)到此,希望大家對圖像特效處理有了一定的認識,了解這些對打好Android游戲開發的基礎很有好處。轉載

『陸』 android 顏色過渡

Android平台下實現漸變效果。在android.graphics中我們可以找到有關Gradient字樣的類,比如LinearGradient 線性漸變、RadialGradient徑向漸變和 角度漸變SweepGradient 三種,他們的基類為android.graphics.Shader。為了顯示出效果,使用一個簡單的例子來說明。

一、LinearGradient線性漸變
在android平台中提供了兩種重載方式來實例化該類分別為,他們的不同之處為參數中第一種方法可以用顏色數組,和位置來實現更細膩的過渡效果,比如顏色采樣int[] colors數組中存放20種顏色,則漸變將會逐一處理。而第二種方法參數僅為起初顏色color0和最終顏色color1。
LinearGradient(float x0, float y0, float x1, float y1, int[] colors, float[] positions, Shader.TileMode tile)
LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, Shader.TileMode tile)
使用實例如下:
Paint p=new Paint();
LinearGradient lg=new LinearGradient(0,0,100,100,Color.RED,Color.BLUE,Shader.TileMode.MIRROR); //
參數一為漸變起初點坐標x位置,參數二為y軸位置,參數三和四分辨對應漸變終點,最後參數為平鋪方式,這里設置為鏡像.
剛才Android開發網已經講到Gradient是基於Shader類,所以我們通過Paint的setShader方法來設置這個漸變,代碼

『柒』 怎麼在android上面做出根據形狀來漸變的效果

<shape> Android:shape=["rectangle" | "oval" | "line" | "ring"] 其中rectagle矩形,oval橢圓,line水平直線,ring環形
<shape>中子節點的常用屬性:
<gradient> 漸變
Android:startColor 起始顏色
Android:endColor 結束顏色
Android:angle 漸變角度,0從左到右,90表示從下到上,數值為45的整數倍,默認為0;
Android:type 漸變的樣式 liner線性漸變 radial環形漸變 sweep <solid > 填充
Android:color 填充的顏色
<stroke >描邊
Android:width 描邊的寬度
Android:color 描邊的顏色
Android:dashWidth 表示'-'橫線的寬度
Android:dashGap 表示'-'橫線之間的距離
<corners >圓角
Android:radius 圓角的半徑 值越大角越圓
Android:topRightRadius 右上圓角半徑
Android:bottomLeftRadius 右下圓角角半徑
Android:topLeftRadius 左上圓角半徑
Android:bottomRightRadius 左下圓角半徑
<padding >填充
android:bottom="1.0dip" 底部填充
android:left="1.0dip" 左邊填充
android:right="1.0dip" 右邊填充
android:top="0.0dip" 上面填充
Selector
根據不同的選定狀態來定義不同的現實效果 分為四大屬性:
android:state_selected 是選中
android:state_focused 是獲得焦點
android:state_pressed 是點擊
android:state_enabled 是設置是否響應事件,指所有事件
android:state_window_focused 默認時的背景圖片 引用位置:res/drawable/文件的名稱.xml
使用的方法:
Java代碼中:R.drawable.文件的名稱 XML中:Android:background="@drawable/文件的名稱"
示例:
<?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/pic_blue"
/>
<!-- 非觸摸模式下獲得焦點並單擊時的背景圖片 -->
<item
Android:state_focused="true"
android:state_pressed="true"
android:drawable= "@drawable/pic_red"
/>
<!-- 觸摸模式下單擊時的背景圖片-->
<item
Android:state_focused="false"
Android:state_pressed="true"
Android:drawable="@drawable/pic_pink"
/>
<!--選中時的圖片背景-->
<item
Android:state_selected="true"
android:drawable="@drawable/pic_orange"
/>
<!--獲得焦點時的圖片背景-->
<item
Android:state_focused="true"
Android:drawable="@drawable/pic_green"
/>
</selector>

layer-list(多個shape)
將多個圖片或上面兩種效果按照順序層疊起來
示例:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="@drawable/android_red"
android:gravity="center" />
</item>
<item android:top="10dp" android:left="10dp">
<bitmap android:src="@drawable/android_green"
android:gravity="center" />
</item>
<item android:top="20dp" android:left="20dp">
<bitmap android:src="@drawable/android_blue"
android:gravity="center" />
</item>
</layer-list>

感覺很像多個drawable
三者可以結合使用
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<layer-list>
<item android:bottom="8.0dip">
<shape>
<solid android:color="#ffaaaaaa" />
</shape>
</item>
<item>
<shape>
<corners android:bottomLeftRadius="4.0dip" android:bottomRightRadius="4.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" />

<solid android:color="#ffaaaaaa" />

<padding android:bottom="1.0dip" android:left="1.0dip" android:right="1.0dip" android:top="0.0dip" />
</shape>
</item>
<item>
<shape>
<corners android:bottomLeftRadius="3.0dip" android:bottomRightRadius="3.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" />

<solid android:color="@color/setting_item_bgcolor_press" />
</shape>
</item>
</layer-list>
</item>
<item>
<layer-list>
<item android:bottom="8.0dip">
<shape>
<solid android:color="#ffaaaaaa" />
</shape>
</item>
<item>
<shape>
<corners android:bottomLeftRadius="4.0dip" android:bottomRightRadius="4.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" />

<solid android:color="#ffaaaaaa" />

<padding android:bottom="1.0dip" android:left="1.0dip" android:right="1.0dip" android:top="0.0dip" />
</shape>
</item>
<item>
<shape>
<corners android:bottomLeftRadius="3.0dip" android:bottomRightRadius="3.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" />

<solid android:color="@color/setting_item_bgcolor" />
</shape>
</item>
</layer-list>
</item>
</selector>

『捌』 android shape怎麼用

下圖是安卓無憂中的例子,可以看裡面的源碼還有文檔,網路一下安卓無憂,然後下載就行,大部分形狀都可以定義,請看截圖:

在Android程序開發中,我們經常會去用到Shape這個東西去定義各種各樣的形狀,首先我們了解一下

Shape下面有哪些標簽,都代表什麼意思:

1.1 solid:填充

android:color指定填充的顏色

1.2 gradient:漸變

android:startColor和android:endColor分別為起始和結束顏色,

android:angle是漸變角度,必須為45的整數倍。

另外漸變默認的模式為android:type="linear",即線性漸變,

可以指定漸變為徑向漸變,android:type="radial",徑向漸變需要指定半徑android:gradientRadius="50"。

angle值對應的位置如圖:

1.3 stroke:描邊

android:width="2dp" 描邊的寬度,android:color 描邊的顏色。

我們還可以把描邊弄成虛線的形式,設置方式為:

android:dashWidth="5dp"

android:dashGap="3dp"

其中android:dashWidth表示'-'這樣一個橫線的寬度,android:dashGap表示之間隔開的距離

1.4 corners:圓角

android:radius為角的弧度,值越大角越圓。

我們還可以把四個角設定成不同的角度,

同時設置五個屬性,則Radius屬性無效

android:Radius="20dp" 設置四個角的半徑

android:topLeftRadius="20dp" 設置左上角的半徑

android:topRightRadius="20dp" 設置右上角的半徑

android:bottomLeftRadius="20dp" 設置右下角的半徑

android:bottomRightRadius="20dp" 設置左下角的半徑

padding:間隔

可以設置上下左右四個方向的間隔

ps:為了方便交流請關注一下我的微博哦哦。

『玖』 怎麼在android上面做出根據形狀來漸變的效果

<shape> Android:shape=["rectangle" "oval" "line" "ring"] 其中rectagle矩形,oval橢圓,line水平直線,ring環形 <shape>中子節點的常用屬性: <gradient> 漸變 Android:startColor 起始顏色 Android:endColor 結束顏色 Android:angle 漸變角度,0從左到右,90表示從下到上,數值為45的整數倍,默認為0; Android:type 漸變的樣式 liner線性漸變 radial環形漸變 sweep <solid > 填充 Android:color 填充的顏色 <stroke >描邊 Android:width 描邊的寬度 Android:color 描邊的顏色 Android:dashWidth 表示'-'橫線的寬度 Android:dashGap 表示'-'橫線之間的距離 <corners >圓角 Android:radius 圓角的半徑 值越大角越圓 Android:topRightRadius 右上圓角半徑 Android:bottomLeftRadius 右下圓角角半徑 Android:topLeftRadius 左上圓角半徑 Android:bottomRightRadius 左下圓角半徑 <padding >填充 android:bottom="1/apk/res/android"> <!-- 默認時的背景圖片--> <item Android:drawable="@drawable/pic1" /> <!-- 沒有焦點時的背景圖片 --> <item Android:state_window_focused="false" android:drawable="@drawable/pic_blue" /> <!-- 非觸摸模式下獲得焦點並單擊時的背景圖片 --> <item Android:state_focused="true" android:state_pressed="true" android:drawable= "@drawable/pic_red" /> <!-- 觸摸模式下單擊時的背景圖片--> <item Android:state_focused="false" Android:state_pressed="true" Android:drawable="@drawable/pic_pink" /> <!--選中時的圖片背景--> <item Android:state_selected="true" android:drawable="@drawable/pic_orange" /> <!--獲得焦點時的圖片背景--> <item Android:state_focused="true" Android:drawable="@drawable/pic_green" /> </selector> layer-list(多個shape) 將多個圖片或上面兩種效果按照順序層疊起來 示例: <?xml version="1/apk/res/android"> <item> <bitmap android:src="@drawable/android_red" android:gravity="center" /> </item> <item android:top="10dp" android:left="10dp"> <bitmap android:src="@drawable/android_green" android:gravity="center" /> </item> <item android:top="20dp" android:left="20dp"> <bitmap android:src="@drawable/android_blue" android:gravity="center" /> </item> </layer-list> 感覺很像多個drawable 三者可以結合使用 <selector xmlns:android="schemas/apk/res/android"> <item android:state_pressed="true"> <layer-list> <item android:bottom="8.0dip"> <shape> <solid android:color="#ffaaaaaa" /> </shape> </item> <item> <shape> <corners android:bottomLeftRadius="4.0dip" android:bottomRightRadius="4.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" /> <solid android:color="#ffaaaaaa" /> <padding android:bottom="1.0dip" android:left="1.0dip" android:right="1.0dip" android:top="0.0dip" /> </shape> </item> <item> <shape> <corners android:bottomLeftRadius="3.0dip" android:bottomRightRadius="3.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" /> <solid android:color="@color/setting_item_bgcolor_press" /> </shape> </item> </layer-list> </item> <item> <layer-list> <item android:bottom="8.0dip"> <shape> <solid android:color="#ffaaaaaa" /> </shape> </item> <item> <shape> <corners android:bottomLeftRadius="4.0dip" android:bottomRightRadius="4.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" /> <solid android:color="#ffaaaaaa" /> <padding android:bottom="1.0dip" android:left="1.0dip" android:right="1.0dip" android:top="0.0dip" /> </shape> </item> <item> <shape> <corners android:bottomLeftRadius="3.0dip" android:bottomRightRadius="3.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" /> <solid android:color="@color/setting_item_bgcolor" /> </shape> </item> </layer-list> </item> </selector>

閱讀全文

與androidgradient漸變相關的資料

熱點內容
倫理電影飛在天上的船 瀏覽:224
求個網址能在線看 瀏覽:548
美國古埃及電影 瀏覽:77
韓國電影成人學院演員有誰 瀏覽:956
美國大胸電影 瀏覽:139
主角重生老北京的小說 瀏覽:199
邵氏100部恐怖影片 瀏覽:100
青春期2裡面的跳舞的歌 瀏覽:36
國產動作愛情片 瀏覽:419
韓國有部特種兵與護士的電影 瀏覽:661
《貪婪》中的日本女演員 瀏覽:477
男主得艾滋病的電影 瀏覽:806
罪孽船長泰國版在線觀看 瀏覽:193
外國電影一個黑男孩在深林 瀏覽:902
叔嫂不倫之戀電影 瀏覽:211
溫暖溫柔是哪部小說 瀏覽:203
穿越抗戰自立為軍閥的小說 瀏覽:601
韓國強殲電影有哪些 瀏覽:291
一女二三男小說穿越文 瀏覽:824
台灣用哪個軟體看電視 瀏覽:365