Android基础知识之控件系列(1)——TextView及自定义动态TextView

来源:互联网 发布:天堂js手机版下载 编辑:程序博客网 时间:2024/06/01 10:51

这一次,我倾尽所有,换你一世陪伴。

今天就算正式开始anroid了,这时候你应该已经有了Java基础,and下载了几个版本的SDK,这些不用我说了吧,如果没有,还是先做做这两项吧。到了控件系列,关系图加代码一放,废话不多说。

先给个View及其子类的树图吧很庞大,一个截图无法全部截取,只能分开。大家也可网上自行下载帮助文档查看View类及其相关子类

运行效果如图:

这里写图片描述

这里写图片描述
ImageView其子类

这里写图片描述

ProgressBar其子类

这里写图片描述

SurfaceView其子类

这里写图片描述

TextView其子类

这里写图片描述

ViewGroup其子类

这里写图片描述

这里写图片描述

这里写图片描述

上面一共8张图片,将整个View类庞大的家族及其关系列了出来。 下面我们就从View的一大子类——TextView开始。直接上代码了。

  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?><ScrollView    android:layout_width="fill_parent"    android:layout_height="wrap_content"    xmlns:android="http://schemas.android.com/apk/res/android" >    <LinearLayout        xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:paddingLeft="@dimen/activity_horizontal_margin"        android:paddingRight="@dimen/activity_horizontal_margin"        android:paddingTop="@dimen/activity_vertical_margin"        android:paddingBottom="@dimen/activity_vertical_margin"        android:orientation="vertical"        android:scrollbars="vertical"        android:isScrollContainer="true">        <TextView            android:text="Hello World! 你好,世界!width为wrap_content"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="18sp"            android:textColor="#CCCCCC"            android:layout_marginBottom="10dp"/>        <TextView            android:text="Hello World! 你好,世界!width为fill_parent"            android:layout_width="fill_parent"            android:background="@drawable/pigs"            android:layout_height="wrap_content"            android:layout_marginBottom="10dp"/>        <TextView            android:text="Hello World! 你好,世界!Hello World! 你好,世界!没有设置singleLine的TextView使用自定义background"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:background="@drawable/setbar_bg"            android:layout_marginBottom="10dp"/>        <TextView            android:text="设置了singleLine的TextView,Hello World! 你好,世界!Hello World! 你好,世界!"            android:singleLine="true"            android:marqueeRepeatLimit="marquee_forever"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_marginBottom="10dp"/>        <TextView            android:text="Hello World! 你好,世界!Hello World! 你好,世界!Hello World! 你好,世界!Hello World! 你好,世界!"            android:singleLine="true"            android:ellipsize="marquee"            android:textIsSelectable="true"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_marginBottom="10dp"/>        <TextView            android:text="drawableLeft和drawableBottom,最后附上一个自定义TextView"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:drawableLeft="@drawable/pigs"            android:drawableBottom="@drawable/pigs"            android:layout_marginBottom="10dp"/>        <com.im.wu.textviewpractice.MyTextView            android:text="Hello World! 你好,世界!Hello World! 你好,世界!Hello World! 你好,世界!Hello World! 你好,世界!"            android:layout_width="fill_parent"            android:layout_height="wrap_content">        </com.im.wu.textviewpractice.MyTextView>        <TextView            android:id="@+id/tv_hello"            android:text="Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!                                Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!                                Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!                                ello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!                                Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!                                Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!"            android:maxLines="5"            android:isScrollContainer="true"            android:scrollbars="vertical"            android:focusable="true"            android:layout_width="wrap_content"            android:layout_height="wrap_content" >        </TextView>    </LinearLayout></ScrollView>
  • MainActivity.java
package com.im.wu.textviewpractice;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.method.ScrollingMovementMethod;import android.view.MotionEvent;import android.view.View;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        TextView textView = (TextView) findViewById(R.id.tv_hello);        textView.setMovementMethod(new ScrollingMovementMethod());        textView.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                // TODO Auto-generated method stub                if (event.getAction() == MotionEvent.ACTION_DOWN) {                    //通知父控件不要干扰                    v.getParent().requestDisallowInterceptTouchEvent(true);                }                if (event.getAction() == MotionEvent.ACTION_MOVE) {                    //通知父控件不要干扰                    v.getParent().requestDisallowInterceptTouchEvent(true);                }                if (event.getAction() == MotionEvent.ACTION_UP) {                    v.getParent().requestDisallowInterceptTouchEvent(false);                }                return false;            }        });    }}

activity_main.xml其中有一个自定义drawable/setbar_bg和一个自定义TextView——MyTextView。下面附上相关代码

  • setbar_bg.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <solid android:color="#FFE4B5"/>    <stroke        android:width="0.5dip"        android:color="#81CE47"/></shape>
  • MyTextView.java
package com.im.wu.textviewpractice;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.LinearGradient;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.Shader;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.View;import android.widget.TextView;/** * Created by wu on 2016/3/7. */public class MyTextView extends TextView {    private int mViewWidth;         //当前view的宽    private int mViewHeight;        //当前view的高    private int mTranslate;    private Paint mPaint;           //画笔    private LinearGradient mLinearGradient; //线性梯度变换    private Matrix mGradientMatrix;         //The Matrix class holds a 3x3 matrix for transforming coordinates.    public MyTextView(Context context) {        super(context, null);    }    //构造方法    public MyTextView(Context context,AttributeSet attrs) {        super(context, attrs);    }    //构造方法    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        if(mGradientMatrix != null){            mTranslate += mViewWidth / 5;            if(mTranslate > 2 * mViewWidth){                mTranslate = -mViewWidth;            }            mGradientMatrix.setTranslate(mTranslate, 0);            /** Set the matrix to translate by (dx, dy). */            mLinearGradient.setLocalMatrix(mGradientMatrix);            /**Set the shader's local matrix. */            postInvalidateDelayed(100);            /**Cause an invalidate to happen on a subsequent cycle through the event              loop. Waits for the specified amount of time.*/        }    }    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        super.onSizeChanged(w, h, oldw, oldh);        if(mViewWidth == 0){            mViewWidth = getMeasuredWidth();            mViewHeight = getMeasuredHeight();            if(mViewWidth > 0){                mPaint = getPaint();                mLinearGradient = new LinearGradient(0, 0, mViewWidth, 0, new int[]{Color.RED, 0xffffffff, Color.BLUE}, null, Shader.TileMode.CLAMP);                /** Create a shader that draws a linear gradient along a line.                 @param x0           The x-coordinate for the start of the gradient line                 @param y0           The y-coordinate for the start of the gradient line                 @param x1           The x-coordinate for the end of the gradient line                 @param y1           The y-coordinate for the end of the gradient line                 @param  colors      The colors to be distributed along the gradient line                 @param  positions   May be null. The relative positions [0..1] of                 each corresponding color in the colors array. If this is null,                 the the colors are distributed evenly along the gradient line.                 @param  tile        The Shader tiling mode                 */                mPaint.setShader(mLinearGradient);                mGradientMatrix = new Matrix();            }        }    }}

部分内容摘自()。

  • 可关注微信公众号(zhudekoudai 、smart_android)
  • QQ群号: 413589216
  • 专注Android分享:http://www.codernote.top/
0 0
原创粉丝点击