Canvas基础
来源:互联网 发布:libevent windows 编译 编辑:程序博客网 时间:2024/05/17 08:45
Canvas基础
import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Paint;import android.os.Bundle;import android.view.Menu;import android.view.MotionEvent;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MyView(this)); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } class MyView extends View { Canvas mCanvas; Bitmap mBitmap; Paint mPaint; float movex, movey; float X_r, Y_r; public MyView(Context context) { super(context); init(); } public void init() { mBitmap = Bitmap.createBitmap(240, 300, Config.ARGB_8888); mCanvas = new Canvas(mBitmap); mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.studycenter_userinfo_back); X_r = -mBitmap.getWidth(); Y_r = -mBitmap.getHeight(); mPaint = new Paint(); mPaint.setColor(0xFFFFFFFF); } protected void onDraw(Canvas canvas) { canvas.drawBitmap(mBitmap, X_r, Y_r, mPaint); System.out.println("onDraw"); super.onDraw(canvas); } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { // 点击时的(x,y),记录初始位置 case MotionEvent.ACTION_DOWN: Animation alphaAnimation = new AlphaAnimation(0.1f, 1.0f); alphaAnimation.setDuration(500); this.startAnimation(alphaAnimation); movex = mBitmap.getWidth() / 2; movey = mBitmap.getHeight() / 2; X_r = x - movex; Y_r = y - movey; mPaint.setAlpha(255); invalidate(); break; // 移动时更新(x,y),同时更新图片 case MotionEvent.ACTION_MOVE: movex = mBitmap.getWidth() / 2; movey = mBitmap.getHeight() / 2; X_r = x - movex; Y_r = y - movey; invalidate(); break; // 弹起时更新(x,y),并记录位置 case MotionEvent.ACTION_UP: mPaint.setAlpha(0); invalidate(); break; } return true; } }}
0 0
- Canvas基础
- Canvas基础
- Canvas基础
- canvas基础
- canvas 基础
- canvas-基础
- canvas 基础
- canvas基础
- canvas基础
- Canvas基础
- canvas基础
- canvas基础
- html5 canvas基础特性
- HTML5 Canvas 基础 一
- HTML5 Canvas 基础 二
- HTML5 canvas 基础入门教程
- Canvas绘制基础
- HTML之Canvas基础
- Android网络通信(一)
- Local Notification
- [VS2010]_[初级]_[常用快捷键]
- 任务管理器中cpu使用记录中显示的窗口个数
- ios 关于推送发布的一些流程
- Canvas基础
- android ListView 几个重要属性
- HandlerThread分线程启动handler
- iOS访问https ssl和tls双向加密
- 使用文件名的间接
- android的Notification学习
- 新手学Android【二】-Android应用程序创建
- 《coredump问题原理探究》Linux x86版第二章coredump捕获的环境配置 转
- adb shell dumpsys activity