自定义控件CustomView(炒股界面 显示 时间与价格 还有折线)
来源:互联网 发布:linux 解压 zip 装什么 编辑:程序博客网 时间:2024/04/29 17:31
CustomView 自定义控件继承View
public class CustomView extends View { int viewWidth, viewHeight; int gap; ArrayList<HashMap<String, String>> list; int maxPrice; int touchY; public CustomView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onTouchEvent(MotionEvent event) {touchY=(int) event.getY();int x=(int) event.getX();Log.i("坐标", x+",gap="+gap);int index=x/gap;Toast.makeText(getContext(), list.get(index).get("time"), Toast.LENGTH_SHORT).show(); return super.onTouchEvent(event); } // textView.setText() public void setData(ArrayList<HashMap<String, String>> list) { this.list = list; for (int i = 0; i < list.size(); i++) { int price = Integer.parseInt(list.get(i).get("price")); if (price > maxPrice) { maxPrice = price; } } } // 画 @Override protected void onDraw(Canvas canvas) { try { Log.i("测试", "onDraw()"); Paint paint = new Paint(); paint.setColor(Color.BLACK); // 画背景 Rect backgroundRect = new Rect(0, 0, viewWidth, viewHeight); canvas.drawRect(backgroundRect, paint); // 画时间 paint.setColor(Color.WHITE); paint.setTextSize(24); String lastTime = list.get(list.size() - 1).get("time"); Rect rect = new Rect(); paint.getTextBounds(lastTime, 0, lastTime.length(), rect); int lastTimeWidth = rect.width(); gap = (viewWidth - lastTimeWidth * 2) / (list.size() - 1); int priceHeight = viewHeight - rect.height(); for (int i = 0; i < list.size(); i++) { String time = list.get(i).get("time"); canvas.drawText(time, i * gap, viewHeight, paint); // 画price String strPrice = list.get(i).get("price"); int intPrice = Integer.parseInt(strPrice); int priceY = intPrice * priceHeight / maxPrice; // 最大的值显示在下面,应该显示在上面 priceY = priceHeight - priceY; // 最大的值显示在控件的外面 priceY = priceY + rect.height(); canvas.drawText(strPrice, i * gap, priceY, paint); // 画线 if (i < list.size() - 1) { int startX = i * gap; int startY = priceY; int stopX = (i + 1) * gap; int stopY = 0; String strNextPrice = list.get(i + 1).get("price"); int nextPriceY = Integer.parseInt(strNextPrice) * priceHeight / maxPrice; nextPriceY = priceHeight - nextPriceY; nextPriceY = nextPriceY + rect.height(); stopY = nextPriceY; canvas.drawLine(startX, startY, stopX, stopY, paint); } } //画线 paint.setColor(Color.RED); if (touchY>=1) { canvas.drawLine(0, touchY, viewWidth, touchY, paint); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); viewHeight = h; viewWidth = w; }}
在activity中
public class MainActivity extends Activity {CustomView customView;Random random=new Random();Handler handler=new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); customView=(CustomView) findViewById(R.id.customView); customView.setData(getData()); handler.postDelayed(new Runnable() { @Override public void run() { Log.i("测试", "run"); customView.setData(getData()); //直接调用 //customView.onDraw(canvas) //屏幕无效--》重新画 调ondraw //间接调用handler.sendMessage, handleMessage() //sendBroadcast onReceiver() //如保让界面更新 //1,textView.setText() //2,adapter.notifyDataSetChanged() //3,invalidate() customView.invalidate(); //延迟1秒 再从新执行这个run handler.postDelayed(this, 1000); } }, 1000); } public ArrayList<HashMap<String, String>> getData() { //9 3000 //10 4000 //11 5000 //13 6000 ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>(); HashMap<String, String> map=new HashMap<String, String>(); map.put("time", "9"); map.put("price", String.valueOf(random.nextInt(5000))); list.add(map); map=new HashMap<String, String>(); map.put("time", "10"); map.put("price", String.valueOf(random.nextInt(5000))); list.add(map); map=new HashMap<String, String>(); map.put("time", "11"); map.put("price", String.valueOf(random.nextInt(5000))); list.add(map); map=new HashMap<String, String>(); map.put("time", "13"); map.put("price", String.valueOf(random.nextInt(5000))); list.add(map); return list; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}
layout 布局文件
0 0
- 自定义控件CustomView(炒股界面 显示 时间与价格 还有折线)
- android ActionBar自定义CustomView
- 用户自定义控件 界面 不显示问题
- 安卓中实现动态显示时间(基础+运用),自定义控件
- Android 自定义价格日历控件
- Android 自定义价格日历控件
- 自定义控件-- 折线图--Android
- 自定义控件之折线图
- 单例模式,自定义cell加长版,对控件的圆润度设置还有另一种跳界面方式(很多界面)
- 54.自定义View练习(二)简易折线图控件
- android自定义控件-----自定义数字价格键盘
- 自定义控件实战<五> 折线图控件
- CustomView
- CustomView
- CustomView
- DateTimePicker控件进行时间选择与显示
- android自定义控件----折线图报表
- Android 自定义控件之折线图
- POJ 1986Distance Queries tarjan求LCA
- 自定义 remoteView
- GUI
- 组播(多播)的C程序实战
- iOS开发最简单的让你的Cell自适应高度
- 自定义控件CustomView(炒股界面 显示 时间与价格 还有折线)
- strstr函数和find函数的异同
- 装饰器
- 第二次作业-在linux下编译
- SDAU 练习三 1014 折线分割平面问题
- Android xml资源文件中@、@android:type、@*、?、@+
- iOS开发-你真的会用SDWebImage?
- JAVA sort
- ORMLite系列---第一篇