码迷,mamicode.com
首页 > 移动开发 > 详细

Android自定义View

时间:2016-04-08 20:01:29      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

自定义view

技术分享

package com.bawei.brush;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class Line extends View {

    public Line(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public Line(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public Line(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        Paint paint = new Paint();//定义画笔
        paint.setColor(Color.RED);//画笔红色
        paint.setTextSize(20);//字体大小
        paint.setStrokeWidth(20);//设置线宽
        canvas.drawColor(Color.BLUE);//画板蓝色
        canvas.drawLine(20, 20, 200, 200, paint);//直线 1.开始X 2.开始Y 3.结束X 4.结束Y 5.画笔
        canvas.drawText("我是被画出来的", 10, 100, paint);//文字 1.显示文字 2.开始X 3.开始Y 4.画笔
        canvas.drawCircle(55, 55, 50, paint);//圆 1.圆心X 2.圆心Y 3.半径 4.画笔
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        setMeasuredDimension(200, 200);//画板宽高
        // super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

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

    <com.bawei.brush.Line
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

技术分享

单点触控小球

自定义Drawview.java

package com.bawei.brush;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class Drawview extends View{
public float X=30;//刚进入位置
public float Y=30;

public Drawview(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
}
 @Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    Paint paint=new Paint();//定义画笔
    paint.setColor(Color.RED);//画笔颜色
    canvas.drawCircle(X, Y, 20, paint);//画小球
    
}
 
 @Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
     //当前组件的X,Y
     this.X=event.getX();
     this.Y=event.getY();
     //通知该组件重绘
     this.invalidate();
    
    return true;
                    
}
}
<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"
     >

    <com.bawei.brush.Drawview
        android:id="@+id/tt"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
      
       />

</LinearLayout>

 技术分享

Android自定义View

标签:

原文地址:http://www.cnblogs.com/1426837364qqcom/p/5369698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!