Android學習筆記之Bitmap位元影像雖觸摸點移動

來源:互聯網
上載者:User

 

package xiaosi.bitmap;import android.app.Activity;import android.os.Bundle;public class mianActivity extends Activity{private surfaceView bitmapView = null;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);bitmapView = new surfaceView(this);setContentView(bitmapView);}}

surfaceView surfaceView .java

package xiaosi.bitmap;import java.io.InputStream;import android.content.Context;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.drawable.BitmapDrawable;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceView;public class surfaceView extends SurfaceView{// 控制surface的介面,提供了控制surface的大小、格式、像素private SurfaceHolder surfaceHolder;private Canvas canvas = null;// x y使用者才觸控螢幕幕的座標private float x=0,y=0;private Bitmap bitmap = null;public surfaceView(Context context) {super(context);// 擷取SurfaceHolder介面surfaceHolder = this.getHolder();// 設定螢幕保持開啟狀態this.setKeepScreenOn(true);// 擷取資源檔resResources res=getResources();// 擷取位元影像資源檔的輸入資料流InputStream inputStream=res.openRawResource(R.drawable.h);// 建立可繪製的位元影像對象BitmapDrawable bitmapDrawable=new BitmapDrawable(inputStream);// 通過可繪製位元影像對象得到位元影像引用bitmap=bitmapDrawable.getBitmap();/* * // 擷取資源檔的引用res          Resources res = getResources();          // 擷取圖形資源檔          Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.h);   */}//繪製位元影像private void onDraw() {try {// 鎖定Canvas畫布canvas = surfaceHolder.lockCanvas();// 設定canvas畫布背景為黑色canvas.drawColor(Color.GREEN);// 在畫布上繪製位元影像//讓位元影像的中心正好在觸摸點位置上        canvas.drawBitmap(bitmap, x-bitmap.getWidth()/2, y-bitmap.getHeight()/2, null);  } catch (Exception ex) {} finally { if (canvas != null)// 解鎖畫布,並顯示繪製圖片surfaceHolder.unlockCanvasAndPost(canvas); }}//觸摸事件 public boolean onTouchEvent(MotionEvent event){ x = event.getX(); y = event.getY(); onDraw();return true;  }}

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.