Hi,
I am getting starange issue on surfaceview but when used view it
worked fine.
1.) when using view and drawing image using
canvas.drawBitmap(mBitmap[CntcurrentImage], 0, 0, null); works
perfectly and draws image fully fitting in the screen.
2.) But when using surfaceview and doing the same thing is streching
the image(not fully fit in the screen).
I am not getting any clue on the issue.
below is the code:
package com.example.HelloGridView;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.MeasureSpec;
public class slideshowview extends Activity {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private long mMoveDelay;
private Integer[] mImageIds = {
R.drawable.wall_img01, R.drawable.wall_img02,
R.drawable.wall_img03, R.drawable.wall_img04,
R.drawable.wall_img05, R.drawable.wall_img06,
R.drawable.wall_img07, R.drawable.wall_img08,
R.drawable.wall_img09, R.drawable.wall_img10,
R.drawable.wall_img11, R.drawable.wall_img12,
R.drawable.wall_img13, R.drawable.wall_img14,
R.drawable.wall_img15
};
public class MySurfaceThread extends Thread {
private SurfaceHolder myThreadSurfaceHolder;
private MySurfaceView myThreadSurfaceView;
private boolean myThreadRun = false;
public MySurfaceThread(SurfaceHolder surfaceHolder, MySurfaceView
surfaceView) {
myThreadSurfaceHolder = surfaceHolder;
myThreadSurfaceView = surfaceView;
}
public void setRunning(boolean b) {
myThreadRun = b;
}
@Override
public void run() {
// TODO Auto-generated method stub
//super.run();
while (myThreadRun) {
Canvas c = null;
try {
c = myThreadSurfaceHolder.lockCanvas(null);
synchronized (myThreadSurfaceHolder) {
myThreadSurfaceView.onDraw(c);
}
sleep(mMoveDelay);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// do this in a finally so that if an exception is
thrown
// during the above, we don't leave the Surface in
an
// inconsistent state
if (c != null) {
myThreadSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
public class MySurfaceView extends SurfaceView implements
SurfaceHolder.Callback{
private MySurfaceThread thread;
private long mLastTick;
boolean isPaused;
private Bitmap[] mBitmap= new Bitmap[15];;
private Bitmap mBitmapClose;
private Bitmap mBitmapPlay;
private Bitmap mBitmapTime;
int CntcurrentImage;
@Override
protected void onMeasure(int widthMeasureSpec, int
heightMeasureSpec) {
// TODO Auto-generated method stub
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
//super.onDraw(canvas);
//Paint p = new Paint();
//p.setAntiAlias(true);
if(!getStatus())
{
canvas.drawBitmap(mBitmap[CntcurrentImage], 0, 0, null);
CntcurrentImage++;
if(CntcurrentImage == 15)
CntcurrentImage = 0;
}
if(getStatus())
{
canvas.drawBitmap(mBitmap[CntcurrentImage], 0, 0, null);
}
canvas.drawBitmap(mBitmapTime,
0,this.getHeight()-30,paint);
canvas.drawBitmap(mBitmapPlay, this.getWidth()/
2-15,this.getHeight()-30,paint);
canvas.drawBitmap(mBitmapClose,
this.getWidth()-30,this.getHeight()-30,paint);
System.out.println("hello");
System.out.println(mMoveDelay);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
//return super.onTouchEvent(event);
float x = event.getX();
float y = event.getY();
//points.add(point);
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
if(x>0&&x<30&&y>(this.getHeight()-30)&&(y<this.getHeight()))
{
System.out.println("clicked");
mMoveDelay = mMoveDelay + 1000;
}
if(x>(this.getWidth()/2-15)&&x<(this.getWidth()/
2+15)&&y>(this.getHeight()-30)&&(y<this.getHeight()))
{
System.out.println("clicked-1");
if(getStatus())
{
setStatus(false);
}
else
{
setStatus(true);
}
}
if(x>(this.getWidth()-30)&&x<(this.getWidth())&&y>(this.getHeight()-30)&&(y<this.getHeight()))
{
System.out.println("clicked-2");
finish();
}
}
return true;
}
public boolean getStatus()
{
return isPaused;
}
public void setStatus(boolean set)
{
isPaused = set;
}
public MySurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
init();
}
public MySurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
public MySurfaceView(Context context, AttributeSet attrs, int
defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
init();
}
private void init(){
getHolder().addCallback(this);
thread = new MySurfaceThread(getHolder(), this);
setFocusable(true); // make sure we get key events
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
paint.setColor(Color.WHITE);
CntcurrentImage = 0;
setFocusable(true);
java.io.InputStream is;
mMoveDelay = 2000;
// Resources r = this.getContext().getResources();
is =
this.getResources().openRawResource(R.drawable.wall_img01);
mBitmap[0] = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.wall_img02);
mBitmap[1] = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.wall_img03);
mBitmap[2] = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.wall_img04);
mBitmap[3] = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.wall_img05);
mBitmap[4] = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.wall_img06);
mBitmap[5] = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.wall_img07);
mBitmap[6] = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.wall_img08);
mBitmap[7] = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.wall_img09);
mBitmap[8] = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.wall_img10);
mBitmap[9] = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.wall_img11);
mBitmap[10] = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.wall_img12);
mBitmap[11] = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.wall_img13);
mBitmap[12] = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.wall_img14);
mBitmap[13] = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.wall_img15);
mBitmap[14] = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.button_close);
mBitmapClose = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.button_play);
mBitmapPlay = BitmapFactory.decodeStream(is);
is =
this.getResources().openRawResource(R.drawable.button_time);
mBitmapTime = BitmapFactory.decodeStream(is);
setStatus(false);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int
arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
thread.setRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
requestWindowFeature(Window.FEATURE_NO_TITLE);
MySurfaceView mySurfaceView = new MySurfaceView(this);
setContentView(mySurfaceView);
}
}
thanks,
mack
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en