开发者

Problem with SurfaceView and its holder

Hi All I tried to use SurfaceView, but the view appeared black and nothing painted Here My code

package com.samples;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Region;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class Galary extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(new GalaryView(this));

    }

    private static class GalaryView extends SurfaceView implements Callback {

        private SurfaceHolder surfaceHolder;

        public GalaryView(Context context) {
            super(context);
            surfaceHolder = getHolder();
            surfaceHolder.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) {
            // TODO Auto-generated method stub
            Effects effects= new Effects(surfaceHolder, this);
            effects.start();
        }

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

        }
    }
}

and the second file

package com.samples;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Effects extends Thread {
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private Bitmap bitmap1;
    private Path mPath;
    private Bitmap bitmap2;
    private Paint paint;

    public Effects(SurfaceHolder surfaceHolder, SurfaceView surfaceView) {
        this.surfaceView = surfaceView;
        this.surfaceHolder = surfaceHolder;
        this.mPath = new Path();
        bitmap1 = BitmapFactory.decodeResource(surfaceView.getContext()
                .getResources(), R.drawable.qina1);
        bitmap2 = BitmapFactory.decodeResource(surfaceView.getContext()
                .getResources(), R.drawable.qina2);

        paint= new Paint();
    }

    @Override
开发者_运维问答    public void run() {
        Canvas canvas = surfaceHolder.lockCanvas();
        canvas.drawColor(Color.WHITE);
        for (int i = 1; i < 100; i++) {
            canvas.drawBitmap(bitmap1, 0, 0, null);
            canvas.save();
            canvas.translate(0, 0);
            mPath.reset();
            canvas.clipPath(mPath);
            int ovalWidth = (int) (surfaceView.getWidth() * (i / 100.0));
            int ovalHeight = (int) (surfaceView.getHeight() * (i / 100.0));
            int ovalX = (surfaceView.getWidth() - ovalWidth) / 2;
            int ovalY = (surfaceView.getHeight() - ovalHeight) / 2;
            mPath.addOval(new RectF(ovalX, ovalY, ovalWidth + ovalX, ovalHeight
                    + ovalY), Path.Direction.CCW);
            canvas.clipPath(mPath, Region.Op.REPLACE);
            canvas.drawBitmap(bitmap2, 0, 0, paint);
            canvas.restore();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

Please could you tell me what is the problem? Thanks a lot


Have a onDraw() inside the class "GalaryView" and draw your graphics there. From the run() of the thread call the onDraw(). This post has some sample code Android:Crash: Binary XML file line : Error inflating class (using SurfaceView) . Hope this helps !

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜