Canvas issue for custom canvas implementation

Put your problem here if it does not fit any of the other categories.

Canvas issue for custom canvas implementation

Postby Soumen's » Wed Dec 19, 2012 1:08 pm

-2 down vote favorite


I am new to android struggling with a issue for last 2 weeks. I have to implement a Canvas. In that canvas I need to implement the Horizontal scroll bar at top of the canvas which will have a list of items. I want to drag and drop the list items on the middle of the Canvas. Tried many things but not able to do that.

I have tried to implement the Canvas by extending the Surfaceview . I have tried to attach the paint object inside the thread but no luck....

!!!PLease help me to come out of this problem loop!!!

The problem image is : http://www.mediafire.com/view/?4k45l15j5qy6eyn

Relevent code for my project : Activity Class:
package com.mango.checkvastugrid;

import net.obviam.checkvastugrid.model.CheckVastuGridPaintObj;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.mango.helper.HelperPakage;
import com.mango.scrollcomponenet.FilterWidgetScoll;
import com.mango.scrollcomponenet.HorizontalScroll.onItemSelectedListener;

public class CheckVastuGridActivity extends Activity {
/** Called when the activity is first created. */

private static final String TAG = CheckVastuGridActivity.class
.getSimpleName();
Paint mPaint = new Paint();
CheckVastuGridPaintObj _droid;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requesting to turn the title OFF
requestWindowFeature(Window.FEATURE_NO_TITLE);
// making it full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set our MainGamePanel as the View
// setContentView(new CheckVastuGridPanel(this));
setContentView(R.layout.main);
Log.d(TAG, "View added");

mPaint.setColor(Color.GREEN);
mPaint.setStrokeWidth(3);
mPaint.setAlpha(0);

FilterWidgetScoll mycanvas2 = (FilterWidgetScoll) findViewById(R.id.SurfaceView02);
mycanvas2.setOnItemSelecetdListener(new onItemSelectedListener() {

private int seleCount = 1;

@Override
public void itemSelected(String text, String strid) {
Toast.makeText(getApplicationContext(),
"id:" + strid + "Text :" + text, Toast.LENGTH_SHORT)
.show();

if (seleCount == 1) {

// DrawCanvas.paint.setAlpha(0);
seleCount--;
} else if (seleCount == 0) {

// DrawCanvas.paint1.setAlpha(150);
// DrawCanvas.paint2.setAlpha(150);
// DrawCanvas.paint3.setAlpha(150);

CheckVastuGridPanel.paint.setColor(Color.RED);
CheckVastuGridPanel.paint.setAlpha(255);


}

}
});

for (int i = 0; i < HelperPakage.tabListHome.length; i++) {
mycanvas2.addText(HelperPakage.tabListHome[i],
HelperPakage.tabListHome[i] + i);
}

mycanvas2.setCurrentSelection(HelperPakage.tabListHome[3]);

}

@Override
protected void onDestroy() {
Log.d(TAG, "Destroying...");
super.onDestroy();
}

@Override
protected void onStop() {
Log.d(TAG, "Stopping...");
super.onStop();
}

}

SurfaceView Class:
/**
*
*/
package com.mango.checkvastugrid;


import net.obviam.checkvastugrid.model.CheckVastuGridPaintObj;
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.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
* @author impaler This is the main surface that handles the ontouch events and
* draws the image to the screen.
*/
public class CheckVastuGridPanel extends SurfaceView implements
SurfaceHolder.Callback {

private static final String TAG = CheckVastuGridPanel.class.getSimpleName();

private MainThread thread;
public static CheckVastuGridPaintObj droid;

static Paint paint;
private Bitmap sweet;
static Canvas mCanvas;

public CheckVastuGridPanel(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub

this.getHolder().addCallback(this);
// this.canvasThread = new CanvasThread(getHolder());
// this.setFocusable(true);
initialize();
}

public CheckVastuGridPanel(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}

private void initialize() {
// adding the callback (this) to the surface holder to intercept events
getHolder().addCallback(this);

// loading the paint object
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(3);
paint.setAlpha(0);

// create droid and load bitmap
sweet = BitmapFactory.decodeResource(getResources(),
R.drawable.check_vastu_inner_grid);
droid = new CheckVastuGridPaintObj(paint, 50, 50);
// droid = new
// CheckVastuGrid(BitmapFactory.decodeResource(getResources(),
// R.drawable.droid_1), 50, 50);

// create the game loop thread
thread = new MainThread(getHolder(), this);

// make the GamePanel focusable so it can handle events
setFocusable(true);
}

public CheckVastuGridPanel(Context context) {
super(context);
initialize();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// at this point the surface is created and
// we can safely start the game loop
thread.setRunning(true);
thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d(TAG, "Surface is being destroyed");
// tell the thread to shut down and wait for it to finish
// this is a clean shutdown
boolean retry = true;
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// try again shutting down the thread
}
}
Log.d(TAG, "Thread was shut down cleanly");
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// delegating event handling to the droid
droid.handleActionDown((int) event.getX(), (int) event.getY());

// check if in the lower part of the screen we exit
if (event.getY() > getHeight() - 50) {
thread.setRunning(false);
((Activity) getContext()).finish();
} else {
Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
}
// invalidate();
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
// the gestures
if (droid.isTouched()) {
// the droid was picked up and is being dragged
droid.setX((int) event.getX());
droid.setY((int) event.getY());
// droid.setTouched(false);
}
// invalidate();
}
if (event.getAction() == MotionEvent.ACTION_UP) {
// touch was released
if (droid.isTouched()) {
droid.setTouched(false);
}
// invalidate();
}
return true;
}

@Override
protected void onDraw(Canvas canvas) {
mCanvas = canvas;
// fills the canvas with black
// canvas.drawColor(Color.BLACK);
canvas.drawBitmap(sweet, 0, 0, null);
droid.drawPaint(canvas, "TEST 1");


canvas.restore();
}


protected void onDraw(Canvas canvas, CheckVastuGridPaintObj checkVastuGridPaintObj){

checkVastuGridPaintObj.drawPaint(canvas , "TEST 2");
}

}
Thread Class:
/**
*
*/
package com.mango.checkvastugrid;

import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;
import com.mango.checkvastugrid.R;


/**
* @author Soumen
*
* The Main thread which contains the game loop. The thread must have access to
* the surface view and holder to trigger events every game tick.
*/
public class MainThread extends Thread {

private static final String TAG = MainThread.class.getSimpleName();

// Surface holder that can access the physical surface
private SurfaceHolder surfaceHolder;
// The actual view that handles inputs
// and draws to the surface
private CheckVastuGridPanel gamePanel;


// flag to hold game state
private boolean running;
public void setRunning(boolean running) {
this.running = running;
}

public MainThread(SurfaceHolder surfaceHolder, CheckVastuGridPanel gamePanel) {
super();
this.surfaceHolder = surfaceHolder;
this.gamePanel = gamePanel;
}

@Override
public void run() {
Canvas canvas;
Log.d(TAG, "Starting game loop");
while (running) {
canvas = null;
// try locking the canvas for exclusive pixel editing
// in the surface
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
// update game state
// render state to the screen
// draws the canvas on the panel
this.gamePanel.onDraw(canvas);
}
} finally {
// in case of an exception the surface is not left in
// an inconsistent state
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
} // end finally
}
}

}
Dragger class:
/**
*
*/
package net.obviam.checkvastugrid.model;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;

import com.mango.checkvastugrid.CheckVastuGridPanel;

/**
* @author Soumen
*
*/
public class CheckVastuGridPaintObj {

private Paint paint; // the actual bitmap
private int x; // the X coordinate
private int y; // the Y coordinate
private boolean touched; // if droid is touched/picked up

public CheckVastuGridPaintObj(Paint paint, int x, int y) {
this.paint = paint;
this.x = x;
this.y = y;
}

public Paint getPaint() {
return paint;
}
public void setPaint(Paint paint) {
this.paint = paint;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}

public boolean isTouched() {
return touched;
}



public void setTouched(boolean touched) {
this.touched = touched;
}

public void drawPaint(Canvas canvas, String strid) {
// canvas.drawBitmap(paint, x - (paint.getWidth() / 2), y - (paint.getHeight() / 2), null);

/* paint.setTextSize(20);

canvas.drawText("Test Text", 150, 150, paint);*/
// String mText = "Text";

paint.setColor(Color.BLACK);


canvas.drawRect(x, y, x+50, y+25, paint);
paint.setColor(Color.WHITE);
paint.setTextSize(20);
canvas.drawText(strid, x, y+25, paint);


}

/**
* Handles the {@link MotionEvent.ACTION_DOWN} event. If the event happens on the
* bitmap surface then the touched state is set to <code>true</code> otherwise to <code>false</code>
* @param eventX - the event's X coordinate
* @param eventY - the event's Y coordinate
*/
/* public void handleActionDown(int eventX, int eventY) {
if (eventX >= (x - paint.getWidth() / 2) && (eventX <= (x + paint.getWidth()/2))) {
if (eventY >= (y - paint.getHeight() / 2) && (y <= (y + paint.getHeight() / 2))) {
// droid touched
setTouched(true);
} else {
setTouched(false);
}
} else {
setTouched(false);
}

}*/
public void handleActionDown(int eventX, int eventY) {
if (eventX >= (x - 50 / 2) && (eventX <= (x + 50/2))) {
if (eventY >= (y - 50/ 2) && (y <= (y + 50 / 2))) {
// droid touched
setTouched(true);
} else {
setTouched(false);
}
} else {
setTouched(false);
}

}
}
Soumen's
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Dec 19, 2012 12:42 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 25 guests