自定义view--绘制图形
2016-09-02 09:58
344 查看
布局文件
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.example.zidinggyiview.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
主Acitivity
package com.example.zidinggyiview;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
// CustomView view=new CustomView(this);
// setContentView(view);
setContentView(R.layout.activity_main);
}
}
编写一个类继承View
package com.example.zidinggyiview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class CustomView extends View {
private static final int WIDTH = 90;//常量半径
private Rect rect = new Rect(90,90,WIDTH,WIDTH);// 绘制矩形的区域
private int deltaX, deltaY;//点击位置和图形边界的偏移量
private static Paint paint = new Paint();// 画笔
public CustomView(Context context) {
super(context);
// TODO Auto-generated constructor stub
paint.setColor(Color.BLUE);// 填充颜色
paint.setAntiAlias(true);
}
public CustomView(Context context, AttributeSet attrs) {
super(context);
// TODO Auto-generated constructor stub
paint.setColor(Color.RED);
paint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
// super.onDraw(canvas);
// 画矩形
//canvas.drawCircle(rect.centerX(),rect.centerY(),WIDTH/2, paint);
//canvas.drawText("画圆:", 10, 20,paint);// 画文本
//canvas.drawCircle(60, 20, 90,paint);// 小圆
paint.setAntiAlias(true);// 设置画笔的锯齿效果。true是去除,大家一看效果就明白了
//canvas.drawCircle(120, 20, 20,paint);// 大圆
//
//canvas.drawText("画矩形:", 10, 80,paint);
paint.setColor(Color.GRAY);// 设置灰色
paint.setStyle(Paint.Style.FILL);//设置填满
canvas.drawRect(60, 60, 80, 80,paint);// 正方形
//canvas.drawRect(60, 90, 160, 100,paint);// 长方形
//
//canvas.drawText("画三角形:", 10, 200,paint);
// // 绘制这个三角形,你可以绘制任意多边形
//
// Path path = new Path();
// path.moveTo(80, 200);// 此点为多边形的起点
// path.lineTo(120, 250);
// path.lineTo(80, 250);
// path.close(); // 使这些点构成封闭的多边形
// canvas.drawPath(path, paint);
//
// //画圆角矩形
// paint.setStyle(Paint.Style.FILL);//充满
// paint.setColor(Color.LTGRAY);
// paint.setAntiAlias(true);// 设置画笔的锯齿效
// canvas.drawText("画圆角矩形:", 10, 260,paint);
// RectF oval3 = new RectF(80, 260, 200, 300);// 设置个新的长方形
// canvas.drawRoundRect(oval3, 20, 15,paint);//第二个参数是x半径,第三个参数是y半径
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (!rect.contains(x, y)) {
return false;// 没有在矩形上点击,不处理触摸消息
}
deltaX = x - rect.left;
deltaY = y - rect.top;
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
Rect old = new Rect(rect);
// 更新矩形的位置
rect.left = x - deltaX;
rect.top = y - deltaY;
rect.right = rect.left + WIDTH;
rect.bottom = rect.top + WIDTH;
old.union(rect);// 要刷新的区域,求新矩形区域与旧矩形区域的并集
invalidate(old);// 出于效率考虑,设定脏区域,只进行局部刷新,不是刷新整个view
break;
}
return true;// 处理了触摸消息,消息不再传递
}
}
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.example.zidinggyiview.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
主Acitivity
package com.example.zidinggyiview;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
// CustomView view=new CustomView(this);
// setContentView(view);
setContentView(R.layout.activity_main);
}
}
编写一个类继承View
package com.example.zidinggyiview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class CustomView extends View {
private static final int WIDTH = 90;//常量半径
private Rect rect = new Rect(90,90,WIDTH,WIDTH);// 绘制矩形的区域
private int deltaX, deltaY;//点击位置和图形边界的偏移量
private static Paint paint = new Paint();// 画笔
public CustomView(Context context) {
super(context);
// TODO Auto-generated constructor stub
paint.setColor(Color.BLUE);// 填充颜色
paint.setAntiAlias(true);
}
public CustomView(Context context, AttributeSet attrs) {
super(context);
// TODO Auto-generated constructor stub
paint.setColor(Color.RED);
paint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
// super.onDraw(canvas);
// 画矩形
//canvas.drawCircle(rect.centerX(),rect.centerY(),WIDTH/2, paint);
//canvas.drawText("画圆:", 10, 20,paint);// 画文本
//canvas.drawCircle(60, 20, 90,paint);// 小圆
paint.setAntiAlias(true);// 设置画笔的锯齿效果。true是去除,大家一看效果就明白了
//canvas.drawCircle(120, 20, 20,paint);// 大圆
//
//canvas.drawText("画矩形:", 10, 80,paint);
paint.setColor(Color.GRAY);// 设置灰色
paint.setStyle(Paint.Style.FILL);//设置填满
canvas.drawRect(60, 60, 80, 80,paint);// 正方形
//canvas.drawRect(60, 90, 160, 100,paint);// 长方形
//
//canvas.drawText("画三角形:", 10, 200,paint);
// // 绘制这个三角形,你可以绘制任意多边形
//
// Path path = new Path();
// path.moveTo(80, 200);// 此点为多边形的起点
// path.lineTo(120, 250);
// path.lineTo(80, 250);
// path.close(); // 使这些点构成封闭的多边形
// canvas.drawPath(path, paint);
//
// //画圆角矩形
// paint.setStyle(Paint.Style.FILL);//充满
// paint.setColor(Color.LTGRAY);
// paint.setAntiAlias(true);// 设置画笔的锯齿效
// canvas.drawText("画圆角矩形:", 10, 260,paint);
// RectF oval3 = new RectF(80, 260, 200, 300);// 设置个新的长方形
// canvas.drawRoundRect(oval3, 20, 15,paint);//第二个参数是x半径,第三个参数是y半径
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (!rect.contains(x, y)) {
return false;// 没有在矩形上点击,不处理触摸消息
}
deltaX = x - rect.left;
deltaY = y - rect.top;
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
Rect old = new Rect(rect);
// 更新矩形的位置
rect.left = x - deltaX;
rect.top = y - deltaY;
rect.right = rect.left + WIDTH;
rect.bottom = rect.top + WIDTH;
old.union(rect);// 要刷新的区域,求新矩形区域与旧矩形区域的并集
invalidate(old);// 出于效率考虑,设定脏区域,只进行局部刷新,不是刷新整个view
break;
}
return true;// 处理了触摸消息,消息不再传递
}
}
相关文章推荐
- 自定义View之Bitmap/在Bitmap上绘制新的图形/Xfermode属性
- Android中使用自定义View实现shape图形绘制
- Android自定义View绘制几何图形
- android自定义View绘制几何图形
- 安卓自定义View进阶-Canvas之绘制图形
- iOS--Quartz2D使用(自定义UIImageView控件、绘制基本图形)
- Android自定义View实现shape图形绘制
- 自定义View-Canvas之绘制图形
- Android绘图系列(二)——自定义View绘制基本图形
- Android Study之自定义View进阶路:掌握绘制基本图形(一)
- android自定义View绘制几何图形
- 自定义VIEW(学习笔记三)-基本图形的绘制
- 安卓自定义View进阶-Canvas之绘制图形
- 在SurfaceView/自定义View中利用手势绘制不同颜色不同形状的图形并且保存为Bitmap位图
- 自定义View,在Canvas上绘制几何图形
- 自定义view绘制几何图形
- Android中图形的绘制-----自定义View(利用canvas和Path相结合)
- 转自大神整理的文章-安卓自定义View进阶-canvas绘制图形
- 自定义View绘制简单基本图形
- 自定义view(二)canvas与图形绘制