美文网首页
Android知识回顾

Android知识回顾

作者: wanTag | 来源:发表于2019-03-29 15:32 被阅读0次

在项目中会用到颜色渐变,我们通过XML实现

创建xml文件

在drawable文件夹下创建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] 根标签,声明一个shape 
[gradient] 声明该shape的属性-渐变色,除此外还有其他属性如corners、stroke、size等等 
[android:angle]渐变色的角度,举例来说,0代表从上至下颜色渐变;45代表从左至右颜色渐变;90代表从下至上颜色渐变… 
[android:startColor&android:endColor] 很好理解,渐变开始的颜色和渐变结束时的颜色(从什么颜色变到什么颜色)
自定义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>

相关文章

  • Android知识回顾

    在项目中会用到颜色渐变,我们通过XML实现 创建xml文件 在drawable文件夹下创建shape资源:shap...

  • Android知识回顾

    Android的启动模式 standard:标准模式 singleTop:栈顶复用模式 singleTask:栈内...

  • android实现手机密码验证

    最近回顾android学习的小项目时,又重新写了一遍android解锁项目,从中回顾了很多知识点,比如界面如何搭建...

  • android-SIP:实战_Register(3)

    基于SIP点对点视频呼叫视频播放 知识点回顾: Android-SIP_开发介绍(1) Android-SIP库:...

  • Android开机流程

    最近回顾的一些知识,补充了一下。 源码标准:API : 29「Android 10.0」 android手机是怎么...

  • Android笔记——ThreadLocal原理浅析

    复习和回顾Android知识,梳理笔记 ThreadLocal简介 ThreadLocal一般在开发中不是很常见,...

  • Android签名验证解析

    1、本文主要内容 知识回顾 签名验证解析 总结 本文介绍下Android在安装apk时,对签名的验证过程 2、知识...

  • Android面试指南:第三章

    前言 终于写到Android高级知识的面试题部分了,前面的知识点只是工具的使用和知识点的回顾,还有就是API调用中...

  • Android基础07-AsyncTask及JSON解析

    一、AsyncTask: (一)、相关知识回顾: 1、开发Android应用时必须遵守单线程模型的原则: Andr...

  • Android面试知识整理-java篇

    Android面试知识整理-android基础知识 Android面试知识整理-性能优化 Android面试知识整...

网友评论

      本文标题:Android知识回顾

      本文链接:https://www.haomeiwen.com/subject/evulbqtx.html