<p>最近项目不是太忙,自己没事近开始研究自定义控件,因为下个版本要用到折线图,就写了个自定义折线图的demo,功能不是太完善,自己意淫一下,有兴趣的小狮子们可以看看。话不多说上代码
public class yuanxing extends View {
/**
* 的颜色
*/
private int mTitleTextColor;
/**
* 绘制时控制文本绘制的范围
*/
private Rect mBound;
private Paint mPaint;
private Paint mPaint2;
private List<mappp> list = new ArrayList<>();
private List<mappp> list2 = new ArrayList<>();
private Canvas canvas;
public yuanxing(Context context) {
super(context);
initPaint();
}
public yuanxing(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initPaint();
}
public yuanxing(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
}
void initPaint() {
mPaint = new Paint();
mPaint2 = new Paint();
list.add(new mappp(100, 100));
list.add(new mappp(100, 200));
list.add(new mappp(100, 300));
list.add(new mappp(100, 400));
list.add(new mappp(100, 500));
list.add(new mappp(100, 600));
list.add(new mappp(200, 600));
list.add(new mappp(300, 600));
list.add(new mappp(400, 600));
list.add(new mappp(500, 600));
list.add(new mappp(600, 600));
list.add(new mappp(700, 600));
list.add(new mappp(800, 600));
list.add(new mappp(900, 600));
list2.add(new mappp(0, 0));
list2.add(new mappp(10, 30));
list2.add(new mappp(100, 100));
list2.add(new mappp(200, 200));
list2.add(new mappp(300, 200));
list2.add(new mappp(400, 400));
list2.add(new mappp(500, 300));
list2.add(new mappp(600, 500));
list2.add(new mappp(700, 100));
list2.add(new mappp(800, 500));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
this.canvas = canvas;
mPaint.reset();
mPaint.setAntiAlias(true);
mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.parseColor("#000000"));
mPaint.setTextSize(30);
mPaint.setStrokeWidth(5);
mPaint2.reset();
mPaint2.setAntiAlias(true);
mPaint2.setFlags(Paint.ANTI_ALIAS_FLAG);
mPaint2.setColor(Color.parseColor("#926DFF40"));
mPaint2.setTextSize(30);
mPaint2.setStrokeWidth(50);
for (int i = 0; i < list.size(); i++) {
canvas.drawCircle(list.get(i).with, list.get(i).height, 10, mPaint);
}
/**
* 纵坐标
* */
for (int i = 0; i <= 5; i++) {
canvas.drawText(5 - i + "k", list.get(i).with - 60, list.get(i).height + 10, mPaint);
}
/**
* 横坐标
* */
for (int i = 5; i < list.size(); i++) {
canvas.drawText(i - 5 + "k", list.get(i).with - 15, list.get(i).height + 60, mPaint);
}
/**
* 折线图
* */
for (int i = 0; i < list2.size() - 1; i++) {
canvas.drawLine(list2.get(i).with + 100, 600 - (list2.get(i).height), list2.get(i + 1).with + 100, 600 - (list2.get(i + 1).height), mPaint);
}
for (int i = 0; i < list2.size(); i++) {
canvas.drawCircle(list2.get(i).with + 100, 600 - list2.get(i).height, 10, mPaint);
}
/**
*柱状图
* */
for (int i = 0; i < list2.size(); i++) {
canvas.drawLine(list2.get(i).with + 100, 600, list2.get(i).with + 100, 600 - (list2.get(i).height), mPaint2);
}
canvas.drawLine(100, 100, 100, 600, mPaint);
canvas.drawLine(100, 600, 950, 600, mPaint);
// canvas.drawLines(foo, mPaint);
}
List<mappp> mapp;
public void setMapp(List<mappp> mapp) {
this.mapp = mapp;
invalidate();
}
class mappp {
int with;
int height;
private mappp mapp;
public mappp(int with, int height) {
this.with = with;
this.height = height;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_DOWN:
//获取屏幕上点击的坐标
float x = event.getX();
float y = event.getY();
//如果坐标在我们的文字区域内,则将点击的文字改颜色
for (int i = 0; i < list2.size(); i++) {
if ((x > list2.get(i).with - 50 + 100 && list2.get(i).with + 50 + 100 > x) && (y > 600 - list2.get(i).height - 50 && 600 - list2.get(i).height + 50 > y)) {
// canvas.drawText("asdasd",1200,1200,mPaint2);
linsenter.Touch(i, list2.get(i));
return true;
}
}
break;
case MotionEvent.ACTION_UP:
//点击抬起后,回复初始位置。
return true;
}
//这句话不要修改
return super.onTouchEvent(event);
}
public interface TouchLinsenter {
void Touch(int position, mappp mappp);
}
private TouchLinsenter linsenter;
public void setLinsenter(TouchLinsenter linsenter) {
this.linsenter = linsenter;
}
public class yuanxing extends View {
/**
* 的颜色
*/
private int mTitleTextColor;
/**
* 绘制时控制文本绘制的范围
*/
private Rect mBound;
private Paint mPaint;
private Paint mPaint2;
private List<mappp> list = new ArrayList<>();
private List<mappp> list2 = new ArrayList<>();
private Canvas canvas;
public yuanxing(Context context) {
super(context);
initPaint();
}
public yuanxing(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initPaint();
}
public yuanxing(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
}
void initPaint() {
mPaint = new Paint();
mPaint2 = new Paint();
list.add(new mappp(100, 100));
list.add(new mappp(100, 200));
list.add(new mappp(100, 300));
list.add(new mappp(100, 400));
list.add(new mappp(100, 500));
list.add(new mappp(100, 600));
list.add(new mappp(200, 600));
list.add(new mappp(300, 600));
list.add(new mappp(400, 600));
list.add(new mappp(500, 600));
list.add(new mappp(600, 600));
list.add(new mappp(700, 600));
list.add(new mappp(800, 600));
list.add(new mappp(900, 600));
list2.add(new mappp(0, 0));
list2.add(new mappp(10, 30));
list2.add(new mappp(100, 100));
list2.add(new mappp(200, 200));
list2.add(new mappp(300, 200));
list2.add(new mappp(400, 400));
list2.add(new mappp(500, 300));
list2.add(new mappp(600, 500));
list2.add(new mappp(700, 100));
list2.add(new mappp(800, 500));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
this.canvas = canvas;
mPaint.reset();
mPaint.setAntiAlias(true);
mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.parseColor("#000000"));
mPaint.setTextSize(30);
mPaint.setStrokeWidth(5);
mPaint2.reset();
mPaint2.setAntiAlias(true);
mPaint2.setFlags(Paint.ANTI_ALIAS_FLAG);
mPaint2.setColor(Color.parseColor("#926DFF40"));
mPaint2.setTextSize(30);
mPaint2.setStrokeWidth(50);
for (int i = 0; i < list.size(); i++) {
canvas.drawCircle(list.get(i).with, list.get(i).height, 10, mPaint);
}
/**
* 纵坐标
* */
for (int i = 0; i <= 5; i++) {
canvas.drawText(5 - i + "k", list.get(i).with - 60, list.get(i).height + 10, mPaint);
}
/**
* 横坐标
* */
for (int i = 5; i < list.size(); i++) {
canvas.drawText(i - 5 + "k", list.get(i).with - 15, list.get(i).height + 60, mPaint);
}
/**
* 折线图
* */
for (int i = 0; i < list2.size() - 1; i++) {
canvas.drawLine(list2.get(i).with + 100, 600 - (list2.get(i).height), list2.get(i + 1).with + 100, 600 - (list2.get(i + 1).height), mPaint);
}
for (int i = 0; i < list2.size(); i++) {
canvas.drawCircle(list2.get(i).with + 100, 600 - list2.get(i).height, 10, mPaint);
}
/**
*柱状图
* */
for (int i = 0; i < list2.size(); i++) {
canvas.drawLine(list2.get(i).with + 100, 600, list2.get(i).with + 100, 600 - (list2.get(i).height), mPaint2);
}
canvas.drawLine(100, 100, 100, 600, mPaint);
canvas.drawLine(100, 600, 950, 600, mPaint);
// canvas.drawLines(foo, mPaint);
}
List<mappp> mapp;
public void setMapp(List<mappp> mapp) {
this.mapp = mapp;
invalidate();
}
class mappp {
int with;
int height;
private mappp mapp;
public mappp(int with, int height) {
this.with = with;
this.height = height;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_DOWN:
//获取屏幕上点击的坐标
float x = event.getX();
float y = event.getY();
//如果坐标在我们的文字区域内,则将点击的文字改颜色
for (int i = 0; i < list2.size(); i++) {
if ((x > list2.get(i).with - 50 + 100 && list2.get(i).with + 50 + 100 > x) && (y > 600 - list2.get(i).height - 50 && 600 - list2.get(i).height + 50 > y)) {
// canvas.drawText("asdasd",1200,1200,mPaint2);
linsenter.Touch(i, list2.get(i));
return true;
}
}
break;
case MotionEvent.ACTION_UP:
//点击抬起后,回复初始位置。
return true;
}
//这句话不要修改
return super.onTouchEvent(event);
}
public interface TouchLinsenter {
void Touch(int position, mappp mappp);
}
private TouchLinsenter linsenter;
public void setLinsenter(TouchLinsenter linsenter) {
this.linsenter = linsenter;
}
}
下载地址:http://download.csdn.net/download/u012941592/9928892