0
我正在製作一個項目,我想要在觸摸時繪製圖像,並且我可以通過觸摸來繪製圖像,但是我想設置我無法設置的透明背景,我使用的代碼是..我如何設置透明背景?
在manifest文件中
<activity
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:name="com.trial.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
代碼...
public class MainActivity extends Activity implements OnTouchListener {
MySurface oursurfaceview;
float x, y;
Bitmap goofy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
oursurfaceview = new MySurface(this);
oursurfaceview.setOnTouchListener(this);
setContentView(oursurfaceview);
x = 0;
y = 0;
goofy = BitmapFactory.decodeResource(getResources(), R.drawable.goofy);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
oursurfaceview.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
oursurfaceview.resume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
x = event.getX();
y = event.getY();
return false;
}
public class MySurface extends SurfaceView implements Runnable {
SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = false;
public MySurface(Context context) {
super(context);
// TODO Auto-generated constructor stub
ourHolder = getHolder();
}
@Override
public void run() {
// TODO Auto-generated method stub
while (isRunning) {
if (!ourHolder.getSurface().isValid())
continue;
Canvas canvas = ourHolder.lockCanvas();
if (x != 0 && y != 0) {
Bitmap test = BitmapFactory.decodeResource(getResources(),
R.drawable.goofy);
canvas.drawBitmap(test, x - (goofy.getWidth()/2), y
- (goofy.getHeight()/2), null);
}
ourHolder.unlockCanvasAndPost(canvas);
}
}
public void pause() {
// TODO Auto-generated method stub
isRunning = false;
while (true) {
try {
ourThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
ourThread = null;
}
public void resume() {
// TODO Auto-generated method stub
isRunning = true;
ourThread = new Thread(this);
ourThread.start();
}
}
}
嘗試。它會幫助你設置透明背景 –
'android:theme =「@ android:style/Theme.Translucent」' 添加此清單將工作良好,直到android 2.3 –
沒有區別,它不設置透明背景。使用上面的代碼一次,你會知道什麼是你使用上面的代碼 –