Comment faire SurfaceView avec un fond transparent?
J'ai mise en page simple
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/backgroundtimer"
android:orientation="vertical" >
<TextView
android:id="@+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/hello" />
<com.fmech.zenclock.surface.ZenClockSurface
android:id="@+id/zenClockSurface1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
Et ZenClockSurface classe
package com.fmech.zenclock.surface;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class ZenClockSurface extends SurfaceView implements SurfaceHolder.Callback{
private DrawClock drawClock;
public ZenClockSurface(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
getHolder().addCallback(this);
}
public ZenClockSurface(Context context) {
super(context);
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
getHolder().addCallback(this);
}
public ZenClockSurface(Context context, AttributeSet attrs) {
super(context, attrs);
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
getHolder().addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
//TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
//this.getHolder().setFormat(PixelFormat.TRANSPARENT);
drawClock = new DrawClock(getHolder(), getResources());
drawClock.setRunning(true);
drawClock.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
//завершаем работу потока
drawClock.setRunning(false);
while (retry) {
try {
drawClock.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
class DrawClock extends Thread{
private boolean runFlag = false;
private SurfaceHolder surfaceHolder;
private Bitmap picture;
private Matrix matrix;
private long prevTime;
private Paint painter;
public DrawClock(SurfaceHolder surfaceHolder, Resources resources){
this.surfaceHolder = surfaceHolder;
this.surfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
picture = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher);
matrix = new Matrix();
this.painter=new Paint();
this.painter.setStyle(Paint.Style.FILL);
}
public void setRunning(boolean run) {
runFlag = run;
}
@Override
public void run() {
Canvas canvas;
while (runFlag) {
matrix.preRotate(1.0f, picture.getWidth() / 2, picture.getHeight() / 2);
canvas = null;
try {
//surfaceHolder.getSurface().setAlpha(0.5f);
canvas = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
canvas.drawColor(Color.TRANSPARENT);
canvas.drawBitmap(picture, matrix, this.painter);
}
}
finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
}
Et le code de l'activité
package com.fmech.zenclock.surface;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
public class ZenClockSurfaceActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.main);
}
}
Je veux que la couleur de fond de android photo a été tranparent mais j'obtiens un fond noir.
J'ai une formation sur RelativeLayout avec une certaine image de la bu SurfaceView faire tourner Android icône avec pas transparent.
Comment je peux le faire transparent?
J'ai oublié d'ajouter ce.getHolder().setFormat(PixelFormat.TRANSLUCIDE);dans les deux du constructeur
OriginalL'auteur Dmitry Nelepov | 2011-12-05
Vous devez vous connecter pour publier un commentaire.
Ouais, je l'ai fait je résoudre le problème
Code d'activité
Surface De Code
C'est du travail.
Il n'a pas de travail pour moi. J'ai mis le fond de plan de la couleur #f0f0f0, mais l'arrière-plan de la surface d'affichage est toujours resté noir.
Nelepov: j'ai essayé le même code. mais n'a pas travaillé pour moi. La couleur de surfaceview reste noir.
je suis juste une surface noire et rien d'autre , toute aide ?
De crédit lorsque le crédit est dû: stackoverflow.com/a/7061396/1176596 🙂
OriginalL'auteur Dmitry Nelepov
De l'API de 3 et 4, vous ne pouvez pas mettre n'importe quoi derrière un
SurfaceView
. Citant Dianne Hackborn:http://groups.google.com/group/android-developers/browse_thread/thread/8d88ef9bb22da574
De l'API à partir de 5 vous pouvez utiliser
setZOrderOnTop
. Le truc, c'est que vous avez à faire dans votre constructeur si elle est appelée avant que la vue est attachée à la fenêtre:mais j'ai toujours des problèmes, maintenant, alors il se transforme place où était l'image est en noir.
Merci de remarquer
setZOrderOnTop
, Rainwork! J'ai édité ma réponse. Laissez-moi savoir si cela fonctionne pour vous.setZOrderOnTop - il de résoudre le problème, mais pas complètement. Puis l'image se transforme, nous avons noir "fantôme" backgground. j'ai répondu à mes quêtes, et de donner le code complet, qui fonctionne très bien pour moi.
Veuillez fournir le code comment surfaceview translucide
OriginalL'auteur chiuki