实现Handler
在main方法中调用步骤1和步骤3,然后中间的代码就可以任意调用handler.postMessage实现和安卓上一样的效果
可以让部分安卓的代码与java共用
目前我用的java1.8编译,更低版本应该也问题不大
public static void main(String[] args) {
// 1.启动looper
Looper.prepare(true);
//你的代码写在这里...
...
DrawWindow window= new DrawWindow();
window.setVisible(true);
...
// 2.发送message事件测试
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
System.out.println("message....");
}
};
handler.sendMessage(new Message());
// 3.循环looper
Looper.loop();
}
实现安卓Bitmap/Canvas
Bitmap bitmap = BitmapFactory.decodeStream(getContext().getResources().getAssets().open("icon.png"));
Paint paint = new Paint();
paint.setAntiAlias(true);
Rect src = new Rect(0,0,140,140);
Rect dst = new Rect(0,0,320,320);
canvas.drawBitmap(bitmap, src, dst, paint);
实现类似安卓View的效果(import android.view.View)
public class TestView extends View{
public TestView(Context context) {
super(context);
initView();
}
private void initView(){
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContext().getResources().getAssets().open("icon.png"));
Paint paint = new Paint();
paint.setAntiAlias(true);
Rect src = new Rect(0,0,140,140);
Rect dst = new Rect(0,0,320,320);
canvas.drawBitmap(bitmap, src, dst, paint);
} catch (IOException e) {
e.printStackTrace();
}
super.onDraw(canvas);
}
}
实现Toast效果
activity = new Activity(this);
Toast.makeText(activity, "测试toast").display();
|