Android_自定义View-和view的监听事件

来源:互联网 发布:1-100的素数c语言 编辑:程序博客网 时间:2024/05/09 14:34
1.自定义view当作标签配置时,必须加上包名
2.反射方式创建UI组件对象时,使用的构造方法为下面两个构造方式之一
View(Context context, AttributeSet attrs):
View(Context context, AttributeSet attrs, int defStyle):

View(Context context)


1 自定义View要学习的是重写3个方法, 写构造方法

(1)onDraw
(2)onMeasure
(3)onLayout

2.自定义View的构造方法和onDraw


3.自定义view在布局文件中使用时,必须把包名加上
<xena.view.MyView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="#ff00ffff"
        />
调用的构造方法是MyView(Context context, AttributeSet attrs)

4.view.invalidate();//触发onDraw方法的执行

5.onMeasure

6.得到View的宽高
 view.getMeasuredHeight();// 得到view的高
 view.getMeasuredWidth();//  得到view的宽

DisplayMetrics dm = new DisplayMetrics();activity.getWindowManager().getDefaultDisplay().getMetrics(dm);int screenW = dm.widthPixels;//这就是屏的宽int screenH = dm.heightPixels;
自定义view写文字“中华人民共和国”
<pre name="code" class="java"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="确定" />    <test.act.MyTextView        android:paddingLeft="40dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>


<pre name="code" class="java">public class MyTextView extends View {    private String text = "中华人民共和国";    private int textSize = 100;    private Context context;    private int textColor = Color.RED;    private int backageColor = Color.BLUE;    public MyTextView(Context context, AttributeSet attrs) {        super(context, attrs);        this.context = context;        this.setBackgroundColor(this.backageColor);    }    private int getHightSize(int measureSpec) {        int size = 0;        int spec_mode = MeasureSpec.getMode(measureSpec);        int spec_size = MeasureSpec.getSize(measureSpec);        if (spec_mode == MeasureSpec.EXACTLY) {            System.out.println("exactly");            size = spec_size;        } else if (spec_mode == MeasureSpec.AT_MOST) {            System.out.println("at_most");            size = textSize;        } else if (spec_mode == MeasureSpec.UNSPECIFIED) {            System.out.println("unspecified");            size = 100;        }        return size;    }    private int getWidthSize(int measureSpec) {        int size = 0;        int spec_mode = MeasureSpec.getMode(measureSpec);        int spec_size = MeasureSpec.getSize(measureSpec);        if (spec_mode == MeasureSpec.EXACTLY) {            System.out.println("exactly");            size = spec_size;        } else if (spec_mode == MeasureSpec.AT_MOST) {            System.out.println("at_most");            size = textSize * text.length();        } else if (spec_mode == MeasureSpec.UNSPECIFIED) {            System.out.println("unspecified");            size = 100;        }        return size;    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        this.setMeasuredDimension(this.getWidthSize(widthMeasureSpec), this.getHightSize(heightMeasureSpec));    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        Paint paint = new Paint();        paint.setTextSize(textSize);        paint.setColor(this.textColor);        System.out.println("核潜艇 =" + this.getMeasuredHeight());        Toast.makeText(this.context, "size" + this.getMeasuredHeight(),                Toast.LENGTH_LONG).show();        // 坐标是左下角        float left = this.getPaddingLeft();        float height = this.getMeasuredHeight();//就是textSize的值。        float bottom = height - this.getPaddingBottom() - height*15/100;        canvas.drawText(text, left, bottom, paint);    }}


用自定义View设置字体变化颜色“<span style="color: rgb(0, 176, 80); font-family: 微软雅黑; font-size: 22px; line-height: 33px;">让文字的颜色每隔一秒种变化一次,即文字颜色为红色和黄色,每隔一秒变化一次,则把MyTextView类改为”</span>
<span style="color: rgb(0, 176, 80); font-family: 微软雅黑; font-size: 22px; line-height: 33px;"></span><pre name="code" class="java">public class MyTextView extends View {    private String text = "中华人民共和国";    private int textSize = 100;    private Context context;    private int textColor = Color.RED;    private int backageColor = Color.BLUE;    public MyTextView(Context context, AttributeSet attrs) {        super(context, attrs);        this.context = context;        this.setBackgroundColor(this.backageColor);    }    private int getHightSize(int measureSpec) {        int size = 0;        int spec_mode = MeasureSpec.getMode(measureSpec);        int spec_size = MeasureSpec.getSize(measureSpec);        if (spec_mode == MeasureSpec.EXACTLY) {            System.out.println("exactly");            size = spec_size;        } else if (spec_mode == MeasureSpec.AT_MOST) {            System.out.println("at_most");            size = textSize;        } else if (spec_mode == MeasureSpec.UNSPECIFIED) {            System.out.println("unspecified");            size = 100;        }        return size;    }    private int getWidthSize(int measureSpec) {        int size = 0;        int spec_mode = MeasureSpec.getMode(measureSpec);        int spec_size = MeasureSpec.getSize(measureSpec);        if (spec_mode == MeasureSpec.EXACTLY) {            System.out.println("exactly");            size = spec_size;        } else if (spec_mode == MeasureSpec.AT_MOST) {            System.out.println("at_most");            size = textSize * text.length();        } else if (spec_mode == MeasureSpec.UNSPECIFIED) {            System.out.println("unspecified");            size = 100;        }        return size;    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        this.setMeasuredDimension(this.getWidthSize(widthMeasureSpec),                this.getHightSize(heightMeasureSpec));    }    int i = 0;    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        int textColor = 0;        i++;        if (i % 2 == 0) {            textColor = this.textColor;        }else {            textColor = Color.YELLOW;        }        Paint paint = new Paint();        paint.setTextSize(textSize);        paint.setColor(textColor);        System.out.println("核潜艇 =" + this.getMeasuredHeight());        Toast.makeText(this.context, "size" + this.getMeasuredHeight(),                Toast.LENGTH_LONG).show();        // 坐标是左下角        float left = this.getPaddingLeft();        float height = this.getMeasuredHeight();// 就是textSize的值。        float bottom = height - this.getPaddingBottom() - height * 15 / 100;        canvas.drawText(text, left, bottom, paint);        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }        this.invalidate();    }}


<span style="font-family: 微软雅黑; font-size: 22px; line-height: 33px;"></span><pre name="code" class="java" style="font-size: 22px; line-height: 33px;"><span style="color:#330033;">在封装类里面的set写invalidate()方法,能给自定义的View跳转传值</span><pre name="code" class="java">package xena.act;import xena.view.MyView;import android.app.Activity;import android.content.Context;import android.content.res.Resources;import android.graphics.Color;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.LinearLayout;public class MainActivity extends Activity implements OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//setContentView(R.layout.activity_main);MyView myView = new MyView(this);myView.setBackgroundColor(Color.YELLOW);setContentView(myView);myView.setR(40);myView.setStr("中华人民共和国");myView.setOnClickListener(this);}@Overrideprotected void onStart() {super.onStart();}@Overridepublic void onClick(View v) {//v是事件源MyView myView = (MyView) v;myView.setStr("华清远见");}}/*******************************************************
<pre name="code" class="java">package xena.view;import xena.act.R;import android.content.Context;import android.content.res.Resources;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;public class MyView extends View {private Context context;private float r = 100;private String str = "小";private final String NS = "http://www.hqyj.com";// 名称空间private boolean flag;public float getR() {return r;}public void setR(float r) {this.r = r;this.invalidate();//触发onDraw方法的执行}public String getStr() {return str;}public void setStr(String str) {this.str = str;//this.invalidate();//触发onDraw方法的执行View的监听事件}public boolean isFlag() {return flag;}public void setFlag(boolean flag) {this.flag = flag;this.invalidate();//触发onDraw方法的执行}// 用于自定义View当作标签时用的public MyView(Context context, AttributeSet attrs) {super(context, attrs);}// 用于new MyView(...)用的public MyView(Context context) {super(context);}// 用于绘制界面上的内容,当界面显示时调用@Overrideprotected void onDraw(Canvas canvas) {// canvas画布对象super.onDraw(canvas);// 创建笔 画Paint paint = new Paint();paint.setColor(Color.BLUE);// 画圆canvas.drawCircle(30, 30, this.r, paint);paint.setColor(Color.RED);if (flag) {canvas.drawCircle(30, 30, 5, paint);}// 画字// 参数1:被绘制的字符串, 参数2,3:指字符串每一个字符的左下角坐标paint.setTextSize(30);// 设置文字大小canvas.drawText(this.str, 30, 30, paint);}}


0 0
原创粉丝点击