LinearGradient實現線性漸變

1、LinearGradient(float x0, float y0, float x1, float y1, int[] colors, float[] positions,Shader.TileMode tile)
參數:
float x0: 漸變相對父容器的起始點x座標
float y0:漸變相對父容器的起始點y座標
float x1:漸變相對父容器的結束點x座標
float y1:漸變相對父容器的結束點y座標
int[] colors:顏色的int 數組
float[] positions: 表示colors數組中幾個顏色的相對位置,是一個float類型的數組,該數組的長度必須與colors數組的長度相同。如果這個參數使用null也可以,這時系統會按照梯度線來均勻分配colors數組中的顏色
Shader.TileMode tile: 渲染器平鋪模式

2、最近有一個需求,如圖:
這裏寫圖片描述
listview的條目背景是淡藍色,但是兩端有些漸變。

3、案例:
給textview設置背景,TextView的寬爲900px,高爲200px,所以採用鏡像Shader.TileMode.MIRROR,漸變從0到450,
這裏寫圖片描述


public class LinearShadeView extends Drawable {

    private LinearGradient linearGradient;
    private Paint paint;

    /**
     * TextView的寬爲900px,高爲200px,所以採用鏡像,漸變從0到450,
     */

    public LinearShadeView() {
        linearGradient = new LinearGradient(0, 0, 450, 0, new int[]{Color.parseColor("#fbfbfb"), Color.parseColor("#b5ede9")},
                new float[]{0, 0.15f}, Shader.TileMode.MIRROR);
        paint = new Paint();
        paint.setShader(linearGradient);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawRect(0, 0, 900, 200, paint);
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        paint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSPARENT;
    }
}

佈局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:fresco="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:gravity="center"
              android:orientation="vertical"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin"
              android:background="#646464"
              tools:context=".view.MainActivity">

        <TextView
            android:layout_width="900px"
            android:layout_height="200px"
            android:text="放你我分身乏術"
            android:textSize="30dp"
            android:textColor="#000000"
            android:id="@+id/tv_demo"
            android:gravity="center"
            />

</LinearLayout>

使用:

public class MainActivity extends Activity {

    @InjectView(R.id.tv_demo)
    TextView mTvDemo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        mTvDemo.setBackground(new LinearShadeView());

    }
}

自定義Drawable也可以實現圓形圖片不同類型的圖片以及帶有動畫的Drawable

發佈了33 篇原創文章 · 獲贊 14 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章