Image isn't drawn again after Touch [duplicate] - java

I have a "Board.java" class, which is an activity, and a "MySurfaceView.java", which is a SurfaceView. I'm drawing some bitmap on the SurfaceView and I want to move it when the screen is touched (to the touched place). it draws the bitmap but after touching, nothing happened.
Thanks in advance!
Log:
Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=539.47266, y[0]=978.45703, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=90656, downTime=88761, deviceId=0, source=0x1002 }
Board.java:
package com.myfirstapplication.owner.appversion1;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by Owner on 15/06/2016.
*/
public class Board extends AppCompatActivity implements View.OnTouchListener {
TextView tv;
MySurfaceView mv;
Bitmap bp;
float x, y;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mv = new MySurfaceView(this);
mv.setOnTouchListener(this);
bp = BitmapFactory.decodeResource(getResources(), R.drawable.missile_cartoon);
x = 0;
y = 0;
setContentView(mv);
}
#Override
protected void onPause() {
super.onPause();
mv.pause();
}
#Override
protected void onResume() {
super.onResume();
mv.resume();
}
#Override
public boolean onTouch(View v, MotionEvent event) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
x = event.getX();
y = event.getY();
return true;
}
}
MySurfaceView.java:
package com.myfirstapplication.owner.appversion1;
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.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
/**
* Created by Owner on 16/06/2016.
*/
public class MySurfaceView extends SurfaceView implements Runnable {
float x, y;
Bitmap bp;
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
public MySurfaceView(Context context) {
super(context);
Bitmap bpOld = BitmapFactory.decodeResource(getResources(), R.drawable.missile_cartoon);
bp = Bitmap.createScaledBitmap(bpOld, 250, 250, true);
x = 0;
y = 0;
holder = getHolder();
}
#Override
public void run() {
while(isItOK){
if(!holder.getSurface().isValid())
continue;
Canvas c = holder.lockCanvas();
c.drawARGB(255,150,150,10);
c.drawBitmap(bp,x-(bp.getWidth()/2),y-(bp.getHeight()/2),null);
holder.unlockCanvasAndPost(c);
}
}
public void pause() {
isItOK = false;
while(true){
try{
t.join();
}
catch (InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}
public void resume() {
isItOK = true;
t = new Thread(this);
t.start();
}
}

Remove TouchListener in Board
Board:
public class Board extends AppCompatActivity{
MySurfaceView mv;
Bitmap bp;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mv = new MySurfaceView(this);
bp = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
setContentView(mv);
}
#Override
protected void onPause() {
super.onPause();
mv.pause();
}
#Override
protected void onResume() {
super.onResume();
mv.resume();
}
}
And in MySurfaceView
public class MySurfaceView extends SurfaceView implements Runnable {
float x, y;
Bitmap bp;
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
public MySurfaceView(Context context) {
super(context);
Bitmap bpOld = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
bp = Bitmap.createScaledBitmap(bpOld, 250, 250, true);
x = 0;
y = 0;
holder = getHolder();
}
#Override
public void run() {
while(isItOK){
if(!holder.getSurface().isValid())
continue;
Canvas c = holder.lockCanvas();
c.drawARGB(255,150,150,10);
c.drawBitmap(bp,x-(bp.getWidth()/2),y-(bp.getHeight()/2),null);
holder.unlockCanvasAndPost(c);
}
}
public void pause() {
isItOK = false;
while(true){
try{
t.join();
}
catch (InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}
public void resume() {
isItOK = true;
t = new Thread(this);
t.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
x = event.getX();
y = event.getY();
// run();
return super.onTouchEvent(event);
}
}
Above work fine for me.

Related

On Touch Method SurfaceView Android doesn't work

I created a SurfaceView (you can see it in the following) and started in from my Main Activity. I overwrote the onTouchEvent method in the SurfaceView and the problem is that the data I want to have logged with Log.d isn't logged, I don't get any Message...
Does anyone have an idea how I can fix this?
My SurfaceView:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback {
private float top;
private float left;
private float bottom;
private float right;
private MainThread thread;
MainGamePanel(Context context) {
super(context);
getHolder().addCallback(this);
thread = new MainThread(getHolder(), this);
setFocusable(true);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
setWillNotDraw(false);
thread.setRunningMode(MainThread.RUNNING);
thread.start();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (retry) {
try {
thread.join();
retry = false;
} catch (Exception e) {
}
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
right = x + 30;
left = x - 30;
top = y - 30;
bottom = y + 30;
Log.d("tag", x + " " + y);
return true;
}
#Override
protected void onDraw(Canvas canvas) {
}
}
My Main Activity:
import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(new MainGamePanel(getApplicationContext()));
}
}
Try changing "getApplicationContext()" to "this" in following part of code:
setContentView(new MainGamePanel(getApplicationContext()));
to
setContentView(new MainGamePanel(this));

OnTouch Listener doesn't change bitmap location

I have a "Board.java" class, which is an activity, and a "MySurfaceView.java", which is a SurfaceView. I'm drawing some bitmap on the SurfaceView and I want to move it when the screen is touched (to the touched place). it draws the bitmap but after touching, nothing happened.
Thanks in advance!
Log:
Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=539.47266, y[0]=978.45703, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=90656, downTime=88761, deviceId=0, source=0x1002 }
Board.java:
package com.myfirstapplication.owner.appversion1;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by Owner on 15/06/2016.
*/
public class Board extends AppCompatActivity implements View.OnTouchListener {
TextView tv;
MySurfaceView mv;
Bitmap bp;
float x, y;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mv = new MySurfaceView(this);
mv.setOnTouchListener(this);
bp = BitmapFactory.decodeResource(getResources(), R.drawable.missile_cartoon);
x = 0;
y = 0;
setContentView(mv);
}
#Override
protected void onPause() {
super.onPause();
mv.pause();
}
#Override
protected void onResume() {
super.onResume();
mv.resume();
}
#Override
public boolean onTouch(View v, MotionEvent event) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
x = event.getX();
y = event.getY();
return true;
}
}
MySurfaceView.java:
package com.myfirstapplication.owner.appversion1;
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.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
/**
* Created by Owner on 16/06/2016.
*/
public class MySurfaceView extends SurfaceView implements Runnable {
float x, y;
Bitmap bp;
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
public MySurfaceView(Context context) {
super(context);
Bitmap bpOld = BitmapFactory.decodeResource(getResources(), R.drawable.missile_cartoon);
bp = Bitmap.createScaledBitmap(bpOld, 250, 250, true);
x = 0;
y = 0;
holder = getHolder();
}
#Override
public void run() {
while(isItOK){
if(!holder.getSurface().isValid())
continue;
Canvas c = holder.lockCanvas();
c.drawARGB(255,150,150,10);
c.drawBitmap(bp,x-(bp.getWidth()/2),y-(bp.getHeight()/2),null);
holder.unlockCanvasAndPost(c);
}
}
public void pause() {
isItOK = false;
while(true){
try{
t.join();
}
catch (InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}
public void resume() {
isItOK = true;
t = new Thread(this);
t.start();
}
}
Remove TouchListener in Board
Board:
public class Board extends AppCompatActivity{
MySurfaceView mv;
Bitmap bp;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mv = new MySurfaceView(this);
bp = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
setContentView(mv);
}
#Override
protected void onPause() {
super.onPause();
mv.pause();
}
#Override
protected void onResume() {
super.onResume();
mv.resume();
}
}
And in MySurfaceView
public class MySurfaceView extends SurfaceView implements Runnable {
float x, y;
Bitmap bp;
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
public MySurfaceView(Context context) {
super(context);
Bitmap bpOld = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
bp = Bitmap.createScaledBitmap(bpOld, 250, 250, true);
x = 0;
y = 0;
holder = getHolder();
}
#Override
public void run() {
while(isItOK){
if(!holder.getSurface().isValid())
continue;
Canvas c = holder.lockCanvas();
c.drawARGB(255,150,150,10);
c.drawBitmap(bp,x-(bp.getWidth()/2),y-(bp.getHeight()/2),null);
holder.unlockCanvasAndPost(c);
}
}
public void pause() {
isItOK = false;
while(true){
try{
t.join();
}
catch (InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}
public void resume() {
isItOK = true;
t = new Thread(this);
t.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
x = event.getX();
y = event.getY();
// run();
return super.onTouchEvent(event);
}
}
Above work fine for me.

Why my view does not get my touch event

I have the following code:
package com.teste;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class ball extends View implements View.OnTouchListener {
Bitmap littleBall;
float x, y;
private int mWidth, mHeight;
private GestureDetector mDetector;
public ball(Context context) {
super(context);
}
public ball(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mDetector = new GestureDetector(this.getContext(), new BallListener());
}
#Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
mWidth = View.MeasureSpec.getSize(widthMeasureSpec);
mHeight = View.MeasureSpec.getSize(heightMeasureSpec);
x = mWidth/2;
y = mHeight/2;
setMeasuredDimension(mWidth, mHeight);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
littleBall = BitmapFactory.decodeResource(getResources(), R.drawable.verde);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(littleBall, 200, 200, false);
canvas.drawBitmap(resizedBitmap, (x - (resizedBitmap.getWidth() / 2)), (y - resizedBitmap.getHeight()/2), null);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(getContext(), "teste", Toast.LENGTH_LONG);
return mDetector.onTouchEvent(event);
}
class BallListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onDown(MotionEvent e) {
return true;
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_UP) {
x = e.getX();
y = e.getY();
invalidate();
}
return super.onSingleTapUp(e);
}
}
}
In a near future i want make the ball moves to a place when i swipe my finger across the screen. But for the start, my goal is make the LittleBall (created using canvas and bitmap) change its position when i touch at the screen. I am using onTouch and a gestureListener. Searching on the net, i found others ways to get a touch like onTouchListener and even looking and Android Developer Help i did not understand what is the difference between those methods...
Can Someone explain in a easy way when i should use those methods and what im doing wrong to not even show my text?

Change the ImageView Layer

Hye there I'm new to OpenCV but somehow good at Android! I have edited a code to change the Camera Background. For the Image to Set at the Background; I have been successfully done with it! Now the problem is that the ImageView comes over the camera screen! I just want my ImageView to come under the Camera I mean at the back of my camera! My code that I'm using is:
import android.util.Log;
import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import java.util.Random;
public class CustomCameraActivity extends Activity implements SurfaceHolder.Callback {
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;
private static final String TAG = "Svetlin SurfaceView";
private ImageView image;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.custom, null);
LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
image=(ImageView)findViewById(R.id.myimage);
image.setAlpha(150);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
Camera.Parameters params = camera.getParameters();
params.setSceneMode(Camera.Parameters.SCENE_MODE_SUNSET);
//ImageView overlay = (ImageView) findViewById(R.id.ImageView01);
//overlay.bringToFront();
camera.setParameters(params);
// tryDrawing(holder);
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
private void tryDrawing(SurfaceHolder holder) {
Log.i(TAG, "Trying to draw...");
Canvas canvas = holder.lockCanvas();
if (canvas == null) {
Log.e(TAG, "Cannot draw onto the canvas as it's null");
} else {
drawMyStuff(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
private void drawMyStuff(final Canvas canvas) {
Random random = new Random();
Log.i(TAG, "Drawing...");
canvas.drawRGB(255, 128, 128);
}
}
The White Image Should Come Behind the Camera as you can see in the screen shot!
The thing is I just need to know that how can I change the Camera Background doing so! Please give me Hints or any Idea so that can help me. THANKS IN ADVANCE!

android drawPoint() not drawing anything

I am an amateur programmer, and have been playing around with android development. I am currently trying my hand at generating a fractal heightmap. In order to test if teh map is generating properly I need to be able to draw the map to the screen. This is where I've been running into trouble.
Here's the code for my MainActivity, DrawMap class (where my onDraw() is), and my CanvasThread() class.
Main Activity:
package com.psstudios.HMG;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.view.View.OnClickListener;
import android.util.Log;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
Button Startgen = null;
Boolean run=true;
private void log(String text) {
Log.d("heightmap",text);
AppendLog app = new AppendLog(text);
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Startgen = (Button) findViewById(R.id.startgen);
log("loop");
Startgen.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
log("Starting world generation");
WorldGen world=new WorldGen(200,200);
log("World generation complete, drawing heightmap");
setContentView(new DrawMap(MainActivity.this, world));
log("Went too far");
}
});
run=false;
}
}
DrawMap:
package com.psstudios.HMG;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.Bitmap.*;
import android.graphics.Paint;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.util.Log;
import android.util.*;
public class DrawMap extends SurfaceView implements SurfaceHolder.Callback
{
int drawtype=1; //variable for which type of map to draw
Bitmap drawbitmap=null;
CanvasThread canvasThread=null;
Boolean run= true;
int Worldx=200;
int Worldy=200;
WorldGen _world=null;
public void log(String text){
Log.d("Heightmap", text);
AppendLog app = new AppendLog(text);
}
public void init(){
log("init();");
canvasThread=new CanvasThread(getHolder(), this);
setFocusable(true);
log("Post-CanvasThread");
}
public DrawMap(Context context){
super(context);
log("DrawMap(Context context)");
getHolder().addCallback(this);
init();
}
public DrawMap(Context context, AttributeSet attrs) {
super(context, attrs);
log("DrawMap(Context context, AttributeSet attrs)");
getHolder().addCallback(this);
init();
}
public DrawMap(Context context,WorldGen world){
super(context);
log("DrawMap(Context context, WorldGen world)");
getHolder().addCallback(this);
_world=world;
init();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format,int width, int height){
log("surfaceChanged()");
}
#Override
public void surfaceCreated(SurfaceHolder holder){
log("Surface Created");
canvasThread.setRunning(true);
canvasThread.run();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder){
log("Surface Destroyed");
Boolean retry = true;
canvasThread.setRunning(false);
while(retry){
try {
canvasThread.join();
retry=false;
} catch (InterruptedException e) {
//we will try again and again
}
}
}
#Override
public void onDraw(Canvas canvas){
log("onDraw()");
Paint paint = new Paint();
Paint paint2 = new Paint();
//canvas.drawColor(Color.BLACK);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(10);
paint2.setColor(Color.BLUE);
for(int x=0; x<Worldx-1; x++){
for(int y=0; y<Worldy-1; y++){
//log(x + " : " + y);
canvas.drawPoint(x+20, y+20, paint);
canvas.drawPoint(x+220, y+220, paint2);
}
}
}
}
CanvasThread:
package com.psstudios.HMG;
import android.graphics.Canvas;
import android.view.*;
import android.util.Log;
public class CanvasThread extends Thread
{
private SurfaceHolder _surfaceHolder;
private DrawMap _drawMap;
private Boolean _run = false;
private void log(String text){
Log.d("HeightmapGen",text);
AppendLog app = new AppendLog(text);
}
public CanvasThread(SurfaceHolder surfaceHolder, DrawMap drawMap){
_surfaceHolder=surfaceHolder;
_drawMap=drawMap;
log("CanvasThread()");
}
public void setRunning(Boolean run){
log("setRunning()");
_run=run;
}
#Override
public void run() {
log("run()");
Canvas c;
while(_run){
c=null;
try{
c=_surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
_drawMap.onDraw(c);
}
} finally {
if(c != null){
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
The program makes it to the onDraw(); function, and runs through the for loop, but nothing is showing up on the screen. I imagine I'm missing something pretty stupid, but I cannot seem to figure out what's wrong.
Thanks in advance for any help you guys can give me.

Categories

Resources