I have an imageView and I draw the diagram in imageView according to the coordinate array. Every diagram has an onclick event.
Activity.java
package com.example.floorexhibitiontest;
import com.floor.DrawView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class HallActivity extends Activity {#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hall);
}
float[][][] points = new float[][][] {
{
{213,264},
{247,232},
{345,338},
{310,371}
},
{
{171,305},
{205,272},
{302,373},
{267,406}
},
{
{571,320},
{606,320},
{606,428},
{571,428}
}
};
LinearLayout layout = (LinearLayout)findViewById(R.id.root);
final DrawView draw = new DrawView(this,points);
layout.addView(draw);
}
DrawView.java
package com.floor;
import com.example.floorexhibitiontest.R;
import android.annotation.SuppressLint;
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.Path;
import android.graphics.Paint.Style;
import android.graphics.Shader.TileMode;
import android.graphics.Shader;
import android.util.DisplayMetrics;
import android.view.View;
public class DrawView extends View{
private float[][][] points = null;
DisplayMetrics metric = new DisplayMetrics();
public DrawView(Context context,float[][][] p) {
super(context);
metric = context.getApplicationContext().getResources().getDisplayMetrics();
points = p;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float density=metric.density;
for(int i = 0; i < points.length; i++){
Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(Color.BLUE);
Path path=new Path();
path.moveTo(points[i][0][0] / density, points[i][0][1] / density);
path.lineTo(points[i][1][0] / density, points[i][1][1] / density);
path.lineTo(points[i][2][0] / density, points[i][2][1] / density);
path.lineTo(points[i][3][0] / density, points[i][3][1] / density);
path.close();
p.setStyle(Style.STROKE);
canvas.drawPath(path, p);
}
}
}
At present effect:
How to let each rectangular drawed produce their own onclick event?
There is an ImageView in Layout file. My purpose is to put the graphics paint on the ImageView. But the result is ImageView can display. The painted graphics can not display. If I hide
ImageView, the image can display.
Ask everyone’s help. Thanks in advice.
Try smth like this:
public boolean onTouch(View v, MotionEvent event) {
if((event.getX(0)>=x_coord) &&
(event.getY(0)>=y_coord) &&
( event.getX(0)<=x_coord + your_rectangle_width) &&
(event.getY(0)<=y_coord + your_rectangle_heigth))
{
//rectangle selected
}
return true;
}
Or use in onTouch() your actual coordinates from points[] array
Related
How can i set onClickListener and onTouchListenere Both on View Class in android?
my code:
package com.example.circledemo.second;
import java.util.ArrayList;
import android.app.Dialog; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.view.MotionEvent; import android.view.View; import android.view.Window; import android.widget.ImageView; import android.widget.TextView;
import com.example.circledemo.R;
public class SecondTCanvas extends View {
public Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); Context contextData; int layoutWidth, layoutHeight;
private Bitmap canvasBitmap;
public SecondTCanvas(Context context, float[] values, int width, int height) { super(context); contextData = context; layoutHeight = height; layoutWidth = width;
System.out.println("=== Layout .... Width : " + layoutWidth + " Height : " + layoutHeight); }
public void onDraw(Canvas c) {
paint.setColor(Color.BLACK); c.drawRect(0, 0, getWidth(), getHeight(), paint); buildDrawingCache(true); canvasBitmap = getDrawingCache(true); }
#Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { // Get x and y coordinates of where the finger touched int pixelX = (int) (Math.floor(event.getX())); int pixelY = (int) (Math.floor(event.getY()));
if (canvasBitmap != null) { }
invalidate(); }
// If the finger is off the screen if (event.getAction() == MotionEvent.ACTION_UP) { // alphaIndex = -1; //This is a flag to reset the view alphas to // normal invalidate(); }
return true;
// return super.onTouchEvent(event); }
}
While using OnTouchListner use return False now you can handle the click in click listner.
I read the article on http://www.curious-creature.org/2013/12/21/android-recipe-4-path-tracing/
Trying to draw arrows with Path which mentioned in the article, but with the following code, I got the half arrow, I already read articles about how to draw arrows on android. This question is more about "what's wrong with the following code." Thanks in advance.
package com.example.linepractice;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PracticeLineView pl = new PracticeLineView(this);
LayoutInflater mInflater = LayoutInflater.from(this);
LinearLayout mainView = (LinearLayout) mInflater.inflate(R.layout.activity_main, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(120, 120, 120, 120);
pl.setLayoutParams(params);
mainView.addView(pl);
setContentView(mainView);
}
}
package com.example.linepractice;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.View;
public class PracticeLineView extends View {
private Paint mPaint;
public PracticeLineView(Context context) {
super(context);
mPaint = new Paint();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.translate(getPaddingLeft(), getPaddingTop() - getPaddingBottom());
canvas.drawPath(makeArrow(140,140), mPaint);
}
private static Path makeArrow(float length, float height) {
Path p = new Path();
p.moveTo(-2.0f, -height / 2.0f);
p.lineTo(length, 0.0f);
p.lineTo(-2.0f, height / 2.0f);
p.lineTo(-2.0f, -height / 2.0f);
p.close();
return p;
}
}
pic:
private static Path makeArrow(float length, float height) {
Path p = new Path();
p.moveTo(-2.0f, 0.0f);
p.lineTo(length, height / 2.0f);
p.lineTo(-2.0f, height);
p.lineTo(-2.0f, 0.0f);
p.close();
return p;
}
I am trying to draw an oval to the screen but all I am getting is a blank white canvas. Not only am I unsure whether I am drawing the oval correctly, but also am getting nothing to the screen. Does anyone know why this is happening, I think the problem is that it isnt getting to the drawing section of the thread...
package hidden;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
public class Surface extends Activity implements OnTouchListener
{
SurfaceView v;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
setRequestedOrientation(1);
v = new SView(this);
v.setOnTouchListener(this);
v.setZOrderOnTop(true);
setContentView(v);
}
public class SView extends SurfaceView implements Runnable
{
Thread t1 = null;
SurfaceHolder holder;
boolean isItOk = false;
public SView(Context context)
{
super(context);
holder = getHolder();
holder.setFormat(PixelFormat.TRANSPARENT);
}
public void run()
{
while(isItOk)
{
if(!holder.getSurface().isValid())
{
continue;
}
Canvas c = holder.lockCanvas();
Paint p = new Paint();
p.setColor(Color.BLUE);
//OvalShape oval = new OvalShape();
//oval.draw(c, p);
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
float leftx = 20;
float topy = 20;
float rightx = 50;
float bottomy = 100;
RectF ovalBounds = new RectF(leftx, topy, rightx, bottomy);
c.drawOval(ovalBounds, paint);
holder.unlockCanvasAndPost(c);
}
}
public void pause()
{
isItOk = false;
while(true)
{
try
{
t1.join();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
break;
}
t1 = null;
}
public void resume()
{
isItOk = true;
t1 = new Thread(this);
t1.start();
}
}
public boolean onTouch(View arg0, MotionEvent arg1)
{
return false;
}
}
I am new to Android.My application requires to redraw a canvas circle over and over again. However, the speed of redraw is less than what I want. How may I increase the same.
My code is as follows:
--> ImagePracActivity.java
package com.pkg.ImagPrac;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class ImagePracActivity extends Activity {
//DrawView drawView;
movement mv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set full screen view
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mv=new movement(getApplicationContext());
setContentView(mv);
mv.requestFocus();
}
}
--> movement.java
package com.pkg.ImagPrac;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
public class movement extends View implements OnTouchListener{
Display display;
float x=0,y=0;
Paint paint=new Paint();
private boolean flag;
public movement(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
display = ((WindowManager)
context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
x = 0;
y = display.getHeight();
}
#Override
protected void onDraw(Canvas canvas) {
if(x<(display.getWidth()/2))
{
canvas.drawCircle(x++, y--, 5, paint);
}
else if(x<(display.getWidth()))
{
canvas.drawCircle(x++, y++, 5, paint);
}
this.invalidate();
}
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
}
A would advice you to use SurfaceView class instead of View, because SurfaceView faster and allows you to have all drawing logic in separate thread. Read more about SurfaceView : http://developer.android.com/guide/topics/graphics/2d-graphics.html
try this
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(5);
I have been trying to make a program draw circles as I touch the screen with no luck, can anyone please tell me how I can do this? or GOOD tutorial that shows me how... I keep getting errors in my code
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.lang.Math;
public class GameView extends View{
private final float x;
private final float y;
private final int r;
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public GameView(Context context, float x, float y, int r) {
super(context);
setFocusable(true);
mPaint.setColor(0xFFFF0000);
this.x = x;
this.y = y;
this.r = r;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(x, y, r, mPaint);
}
}
GameActivity.java
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
public class GameActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FrameLayout main = (FrameLayout) findViewById(R.id.my_view);
main.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(final View v, MotionEvent e) {
final float x = e.getX();
final float y = e.getY();
final Handler handler = new Handler()
{
public void handleMessage(Message msg) {
}
};
Thread graphicThread = new Thread()
{
public void run() {
try {
//Do the drop
FrameLayout flView = (FrameLayout) v;
GameView gm = new GameView(getParent(), x,y,25);
flView.addView(gm);
Thread.sleep(1000);
handler.sendMessage(handler.obtainMessage());
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
boolean isRunning=true;
graphicThread.start();
return true;
}
});
}
}
You shouldn't be using getParent() in your activity if it's not a child Activity.
Use this instead:
GameView gm = new GameView(this, x, y, 25);
As the NPE question is already answered, I want to provide an answer for the tutorial question. I have written a series (which is partly updated) which you can find here: I have written a tutorial about 2d graphics for a game. Read this: http://www.droidnova.com/2d-tutorial-series