今天闲来无事想了下swing的UI实现,总感觉跟android设计思路不同, swing可以层层嵌套,一个容器套一个容器
而android确只能通过xml来实现,而且做游戏的话,用系统控件几乎不太可能。 因为view不能包含view,
因为自己想实现一个小例子,那就是在view中自己实现一个button类, 从而就像添加系统组件那样添加。
开始毫无思路,因为监听那块我java底子比较薄,偷看了下swing的源码,还是蒙蒙的,swing太多了。
后来翻到了一个高手的博客。询问了一翻。 有了点思路。于是自己动手试了试。虽然实现方法很二
但是通过这个,让自己加深了对接口的了解。 还是基础重要啊,哎
还是先上图
只实现了文字。
package com.example.test1;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Paint.Align;import android.graphics.Paint.FontMetrics;import android.graphics.Rect;import android.view.MotionEvent;public class JButton extends JComponent{ Canvas canvas; private String Text; private Rect rect; public JButton(String text){ this.Text=text; } public String getText() { return Text; } public void setText(String text) { Text = text; } @Override public void paint(Canvas canvas) { // TODO Auto-generated method stub Paint paint=new Paint(); FontMetrics fMetrics=paint.getFontMetrics(); float x=getWidth()/2; float y=getHeight()/2-(fMetrics.ascent+fMetrics.descent)/2; paint.setTextAlign(Align.CENTER); rect=new Rect(getX(), getY(), getX()+getWidth(), getY()+getHeight()); paint.setColor(Color.BLUE); canvas.drawRect(rect, paint); paint.setColor(Color.WHITE); paint.setTextSize(getWidth()/4); canvas.drawText(getText(), x+getX(), y+getY(), paint); } @Override public void repaint() { // TODO Auto-generated method stub paint(canvas); } public Boolean isCicked(MotionEvent event){ return rect.contains((int)event.getX(), (int)event.getY()); }}
package com.example.test1;import android.graphics.Canvas;public abstract class JComponent { private int x; private int y; private int width,height; public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public TouchListener touchListener; public void setSize(int w,int h){ this.width=w; this.height=h; } public void setLocation(int x,int y){ this.x=x; this.y=y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public void addTouchListener(TouchListener t){ this.touchListener=t; } public abstract void paint(Canvas canvas); public abstract void repaint();}
package com.example.test1;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.graphics.Canvas;import android.util.Log;import android.view.MotionEvent;import android.view.View;public class MyView extends View{ public static List<JComponent> container=new ArrayList<JComponent>(); public MyView(Context context) { super(context); //container=new ArrayList<JComponent>(); JButton jButton=new JButton("hello"); jButton.setSize(100, 50); jButton.setLocation(30, 30); jButton.addTouchListener(new TouchListener() { @Override public void onClick() { // TODO Auto-generated method stub Log.i("mylog", "被点击"); } }); container.add(jButton); invalidate(); } //绘图函数 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for(int i=0,len=container.size();i<len;i++) { if(container.get(i) instanceof JButton) { JButton jButton=(JButton)container.get(i); jButton.paint(canvas); } } } //触摸事件 @Override public boolean onTouchEvent(MotionEvent event) { for(int i=0,len=container.size();i<len;i++) { if(container.get(i) instanceof JButton) { JButton jButton=(JButton)container.get(i); if(jButton.isCicked(event)) jButton.touchListener.onClick(); } } return super.onTouchEvent(event); } }
package com.example.test1;public interface TouchListener { public void onClick();}