当前位置: 代码迷 >> Android >> android MySrufaceView 运用
  详细解决方案

android MySrufaceView 运用

热度:432   发布时间:2016-05-01 15:14:31.0
android MySrufaceView 应用
package shuidi.co.cc;


import java.util.Random;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class MySurfaceView extends SurfaceView implements
SurfaceHolder.Callback {
float left;
float top;


Bitmap bitmap = BitmapFactory.decodeResource(getContext()
.getResources(), R.drawable.poolbird);

Bitmap bitmap1 = BitmapFactory.decodeResource(getContext()
.getResources(), R.drawable.shuidi1);




public MySurfaceView(Context context) {
super(context);


getHolder().setFormat(PixelFormat.TRANSPARENT);
getHolder().addCallback(this);


setFocusable(false);
// TODO Auto-generated constructor stub
}


@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub


left = event.getX();
top = event.getY();
doDraw();


return super.onTouchEvent(event);
}


private void doDraw() {
// TODO Auto-generated method stub


Canvas canvas = getHolder().lockCanvas();
canvas.drawBitmap(bitmap1, left, top, null);
canvas.drawBitmap(bitmap, left, top, null);
getHolder().unlockCanvasAndPost(canvas);


}


public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub


}


public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub


doDraw();


}


public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub


}




}



-----------------------------maniferst.xml
    <activity
             android:configChanges="orientation|keyboardHidden"
            android:name=".MainActivity"
            android:theme="@android:style/Theme.Dialog" >


 <activity
            android:name=".shuidi"
            android:theme="@style/Theme.Translucent" >
        </activity>



--------styles
<resources>





    <drawable name="translucent_background">#77ffffff</drawable>


    <style name="Theme.Translucent" parent="android:style/Theme.Translucent">
        <item name="android:windowBackground">@drawable/translucent_background
</item>
    </style>


</resources>

  相关解决方案