Android SurfaceView 透明和半透明

透明//Transparent 半透明//Translucent


package rw.Trans; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.PixelFormat; import android.os.Bundle; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; public class TransParentActivity extends Activity { /** Called when the activity is first created. */ class MySurfaceView extends SurfaceView implements Callback{ float left,top; Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.reen); public MySurfaceView(Context context) { super(context); // TODO Auto-generated constructor stub getHolder().setFormat(PixelFormat.TRANSPARENT);//设置为透明 //getHolder().setFormat(PixelFormat.TRANSLUCENT);//半透明 getHolder().addCallback(this); setFocusable(true); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub doDraw(); } private void doDraw() { // TODO Auto-generated method stub Canvas canvas=getHolder().lockCanvas(); canvas.drawBitmap(bitmap, left, top, null); getHolder().unlockCanvasAndPost(canvas); } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } /* (non-Javadoc) * @see android.app.Activity#onTouchEvent(android.view.MotionEvent) */ @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub left=event.getX(); top=event.getY(); doDraw(); return super.onTouchEvent(event); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MySurfaceView(getApplicationContext())); } }


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="rw.Trans" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" />

<application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".TransParentActivity" android:label="Transparent SurfaceView" android:theme="@android:style/Theme.Translucent" >

<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

</application> </manifest>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值