Android知識(shí)回顧

在項(xiàng)目中會(huì)用到顏色漸變,我們通過(guò)XML實(shí)現(xiàn)

創(chuàng)建xml文件

在drawable文件夾下創(chuàng)建shape資源:
shape_gradient.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/colorAccent"
        android:startColor="@color/colorPrimary" />
</shape>

注:

[shape] 根標(biāo)簽,聲明一個(gè)shape 
[gradient] 聲明該shape的屬性-漸變色,除此外還有其他屬性如corners、stroke、size等等 
[android:angle]漸變色的角度,舉例來(lái)說(shuō),0代表從上至下顏色漸變;45代表從左至右顏色漸變;90代表從下至上顏色漸變… 
[android:startColor&android:endColor] 很好理解,漸變開始的顏色和漸變結(jié)束時(shí)的顏色(從什么顏色變到什么顏色)
自定義View

MyView.java

public class MyView extends View {

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //獲取View的寬高
        int width = getWidth();
        int height = getHeight();

        int colorStart = getResources().getColor(R.color.red);
        int color1 = Color.GRAY;
        int colorEnd = getResources().getColor(R.color.star_yellow);

        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);
    }
}

使用在xml中:

<?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"
    tools:context=".MainActivity">

    <com.yunlin.xihai.user.gradient.view.MyView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android的啟動(dòng)模式 standard:標(biāo)準(zhǔn)模式 singleTop:棧頂復(fù)用模式 singleTask:棧內(nèi)...
    wanTag閱讀 248評(píng)論 0 0
  • 很早看過(guò)這篇文章,并做了筆記,后來(lái)看到群里的小伙伴有問(wèn)相關(guān)Drawable的問(wèn)題,就把這篇翻譯過(guò)來(lái)的文章給放出來(lái)了...
    Kotyo閱讀 1,701評(píng)論 0 5
  • 更多Android總結(jié)知識(shí)點(diǎn) Android中的13種Drawable小結(jié) Android的八種對(duì)話框的實(shí)現(xiàn) An...
    侯蛋蛋_閱讀 4,170評(píng)論 0 5
  • 1. Drawable 簡(jiǎn)介 Drawable 在 Android 開發(fā)中是非常常用的,比如在 XML 中定義co...
    Kip_Salens閱讀 1,341評(píng)論 0 8
  • 東野圭吾先生是我在高中時(shí)就很喜歡的一位作家,他的懸疑小說(shuō)絲絲入扣,特別是最后的轉(zhuǎn)折,讓我欲罷不能。《解憂雜貨店》是...
    湖畔的鈺閱讀 908評(píng)論 0 1

友情鏈接更多精彩內(nèi)容