预期总体效果图(模仿股票):
先画底部时间:
效果图:
Activity
public class MainActivity extends Activity {
CustomView customView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customView=(CustomView) findViewById(R.id.customView);
customView.setData(getData());
}
public ArrayList<HashMap<String, String>> getData()
{
ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>();
HashMap<String, String> map1=new HashMap<String, String>();
map1.put("time", "9");
map1.put("price", "2000");
list.add(map1);
HashMap<String, String> map2=new HashMap<String, String>();
map2.put("time", "10");
map2.put("price", "3000");
list.add(map2);
HashMap<String, String> map3=new HashMap<String, String>();
map3.put("time", "11");
map3.put("price", "5000");
list.add(map3);
HashMap<String, String> map4=new HashMap<String, String>();
map4.put("time", "13");
map4.put("price", "6000");
list.add(map4);
return list;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
自定义View
package com.tarena.customview1504_02;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.graphics.Canvas;
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 {
int viewWidth, viewHeight;
ArrayList<HashMap<String, String>> list = null;
int xGap;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setData(ArrayList<HashMap<String, String>> list) {
this.list = list;
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(0xFF000000);
paint.setTextSize(24);
Rect rectBackground = new Rect(0, 0, viewWidth, viewHeight);
canvas.drawRect(rectBackground, paint);
int lastPriceWidth = 48;
xGap = (viewWidth - lastPriceWidth) / (list.size() - 1);
paint.setColor(0xFFFFFFFF);
for (int i = 0; i < list.size(); i++) {
HashMap<String, String> map = list.get(i);
String time = map.get("time");
int timeLeft = xGap * i;
int timeTop = viewHeight;
canvas.drawText(time, timeLeft, timeTop, paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
viewWidth = w;
viewHeight = h;
}
}
画价格
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.graphics.Canvas;
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 {
int viewWidth, viewHeight;
ArrayList<HashMap<String, String>> list = null;
int xGap;
int maxPrice;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setData(ArrayList<HashMap<String, String>> list) {
this.list = list;
for (int i = 0; i < this.list.size(); i++) {
HashMap<String, String> map = list.get(i);
int currentPrice = Integer.parseInt(map.get("price"));
if (currentPrice > maxPrice) {
maxPrice = currentPrice;
}
}
}
@Override
protected void onDraw(Canvas canvas) {
try {
Paint paint = new Paint();
paint.setColor(0xFF000000);
paint.setTextSize(24);
Rect rectBackground = new Rect(0, 0, viewWidth, viewHeight);
canvas.drawRect(rectBackground, paint);
int lastPriceWidth = 48;
int timeHeight = 40;
int priceHeight = viewHeight - timeHeight;
xGap = (viewWidth - lastPriceWidth) / (list.size() - 1);
paint.setColor(0xFFFFFFFF);
for (int i = 0; i < list.size(); i++) {
HashMap<String, String> map = list.get(i);
String time = map.get("time");
int timeLeft = xGap * i;
int timeTop = viewHeight;
canvas.drawText(time, timeLeft, timeTop, paint);
int currentPrice = Integer.parseInt(map.get("price"));
int priceTop = currentPrice * priceHeight / maxPrice;
priceTop = priceHeight - priceTop;
priceTop = priceTop + 24;
canvas.drawText(map.get("price"), timeLeft, priceTop, paint);
if (i < list.size() - 1) {
int startLeft = timeLeft;
int startTop = priceTop;
int endLeft = (i + 1) * xGap;
int endTop = 0;
int nextPrice = Integer.parseInt(list.get(i + 1).get(
"price"));
int nextTop = nextPrice * priceHeight / maxPrice;
nextTop = priceHeight - nextTop;
nextTop = nextTop + 24;
endTop = nextTop;
canvas.drawLine(startLeft, startTop, endLeft, endTop, paint);
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
viewWidth = w;
viewHeight = h;
}
}
增加需求:当点击时出现横线
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.graphics.Canvas;
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 {
int viewWidth, viewHeight;
ArrayList<HashMap<String, String>> list = null;
int xGap;
int maxPrice;
int touchY;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setData(ArrayList<HashMap<String, String>> list) {
this.list = list;
for (int i = 0; i < this.list.size(); i++) {
HashMap<String, String> map = list.get(i);
int currentPrice = Integer.parseInt(map.get("price"));
if (currentPrice > maxPrice) {
maxPrice = currentPrice;
}
}
}
@Override
protected void onDraw(Canvas canvas) {
try {
Paint paint = new Paint();
paint.setColor(0xFF000000);
paint.setTextSize(24);
Rect rectBackground = new Rect(0, 0, viewWidth, viewHeight);
canvas.drawRect(rectBackground, paint);
int lastPriceWidth = 48;
int timeHeight = 40;
int priceHeight = viewHeight - timeHeight;
xGap = (viewWidth - lastPriceWidth) / (list.size() - 1);
paint.setColor(0xFFFFFFFF);
for (int i = 0; i < list.size(); i++) {
HashMap<String, String> map = list.get(i);
String time = map.get("time");
int timeLeft = xGap * i;
int timeTop = viewHeight;
canvas.drawText(time, timeLeft, timeTop, paint);
int currentPrice = Integer.parseInt(map.get("price"));
int priceTop = currentPrice * priceHeight / maxPrice;
priceTop = priceHeight - priceTop;
priceTop = priceTop + 24;
canvas.drawText(map.get("price"), timeLeft, priceTop, paint);
if (i < list.size() - 1) {
int startLeft = timeLeft;
int startTop = priceTop;
int endLeft = (i + 1) * xGap;
int endTop = 0;
int nextPrice = Integer.parseInt(list.get(i + 1).get(
"price"));
int nextTop = nextPrice * priceHeight / maxPrice;
nextTop = priceHeight - nextTop;
nextTop = nextTop + 24;
endTop = nextTop;
canvas.drawLine(startLeft, startTop, endLeft, endTop, paint);
}
}
if (touchY > 0) {
canvas.drawLine(0, touchY, viewWidth, touchY, paint);
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
touchY = (int) event.getY();
this.invalidate();
return super.onTouchEvent(event);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
viewWidth = w;
viewHeight = h;
}
}
以Handler方式让数据控件实时变化:
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.graphics.Canvas;
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 {
int viewWidth, viewHeight;
ArrayList<HashMap<String, String>> list = null;
int xGap;
int maxPrice;
Random random=new Random();
Handler handler=new Handler();
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
handler.postDelayed(new Runnable){
public void run(){
customview.setData(getData);
customview.invalidate();
handler.postDelayed(this,1000);
}
},1000);
}
public void setData(ArrayList<HashMap<String, String>> list) {
this.list = list;
for (int i = 0; i < this.list.size(); i++) {
HashMap<String, String> map = list.get(i);
int currentPrice = Integer.parseInt(map.get("price"));
if (currentPrice > maxPrice) {
maxPrice = currentPrice;
}
}
}
@Override
protected void onDraw(Canvas canvas) {
try {
Paint paint = new Paint();
paint.setColor(0xFF000000);
paint.setTextSize(24);
Rect rectBackground = new Rect(0, 0, viewWidth, viewHeight);
canvas.drawRect(rectBackground, paint);
int lastPriceWidth = 48;
int timeHeight = 40;
int priceHeight = viewHeight - timeHeight;
xGap = (viewWidth - lastPriceWidth) / (list.size() - 1);
paint.setColor(0xFFFFFFFF);
for (int i = 0; i < list.size(); i++) {
HashMap<String, String> map = list.get(i);
String time = map.get("time");
int timeLeft = xGap * i;
int timeTop = viewHeight;
canvas.drawText(time, timeLeft, timeTop, paint);
int currentPrice = Integer.parseInt(map.get("price"));
int priceTop = currentPrice * priceHeight / maxPrice;
priceTop = priceHeight - priceTop;
priceTop = priceTop + 24;
canvas.drawText(map.get("price"), timeLeft, priceTop, paint);
if (i < list.size() - 1) {
int startLeft = timeLeft;
int startTop = priceTop;
int endLeft = (i + 1) * xGap;
int endTop = 0;
int nextPrice = Integer.parseInt(list.get(i + 1).get(
"price"));
int nextTop = nextPrice * priceHeight / maxPrice;
nextTop = priceHeight - nextTop;
nextTop = nextTop + 24;
endTop = nextTop;
canvas.drawLine(startLeft, startTop, endLeft, endTop, paint);
}
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
viewWidth = w;
viewHeight = h;
}
}
推荐图标框架achartengine: