android 顏色漸變,透明度顏色漸變計算獲取新的透明顏色
阿新 • • 發佈:2019-01-11
一.通過LinearGradient漸變效果
/** @param x0 起始點X座標 @param y0 起始點Y座標 @param x1 終點X座標 @param y1 終點Y座標 @param colors 所有顏色漸變集合 @param positions 我們可以讓它均勻的漸變,也可以讓它按照你想要的比例進行漸變,可以為null,這樣的話假設1為整個漸變的長度,我們設定的所有顏色(假設有4種顏色),都以同等的權重(漸變長度比例0.25:0.25:0.25:0.25)進行顏色漸變。 @param tile 著色器的不同模式 */ public LinearGradient(float x0, float y0, float x1, float y1, int colors[], float positions[], TileMode tile)
著色器三種模式
(1).LinearGradient.TileMode.CLAMP,顏色平鋪
(2).LinearGradient.TileMode.REPEAT,顏色重複著色
(3).LinearGradient.TileMode.MIRROR,顏色映象(對稱效果)
2.如下效果實現方式
1.在佈局中直接使用
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/shape" <!--將整個View賦予漸變色-->> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> 在drawable目錄下新建一個shape.xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:angle="90" android:endColor="@color/colorStart"<!--漸變起始顏色-->> android:startColor="@color/colorEnd" /><!--漸變結束顏色-->> </shape>
2.在自定義佈局中使用
public class SelfView extends View { public SelfView(Context context) { super(context); } public SelfView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public SelfView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //獲取View的寬高 int width = getWidth(); int height = getHeight(); int colorStart = getResources().getColor(R.color.colorPrimary); int color1 = Color.GRAY; int colorEnd = getResources().getColor(R.color.colorAccent); Paint paint = new Paint(); LinearGradient backGradient = new LinearGradient(0, 0, 0, height, new int[]{colorStart, color1 ,colorEnd}, null, Shader.TileMode.CLAMP); paint.setShader(backGradient); canvas.drawRect(0, 0, width, height, paint); } }
二.根據透明度和顏色漸變計算獲取新的顏色
通過ArgbEvaluator獲取顏色漸變值
/**
@param positionOffset:表示漸變度,取0.0F-1.0F之間某一值
@param PAGE_COLOR_ONE:表示起始顏色值
@param PAGE_COLOR_TWO:表示最終顏色值
@param currentLastColor:表示由以上三個引數計算得到的漸變顏色值
@param colors 所有顏色漸變集合
@param positions 我們可以讓它均勻的漸變,也可以讓它按照你想要的比例進行漸變,可以為
**/
ArgbEvaluator argbEvaluator = new ArgbEvaluator();//漸變色計算類
int currentLastColor = (int) (argbEvaluator.evaluate(positionOffset, PAGE_COLOR_ONE, PAGE_COLOR_TWO));
我們只需要在拖拽進度時將透明度或進度轉為0.0F-1.0F作為alpha引數,再給出startcolor和endcolor顏色即可算出新的透明度顏色再將新的顏色設定給其他view
int bgAlpha = Color.alpha((int) mEvaluator.evaluate(alpha, startcolor, endcolor));