Photoview ondraw - android

I am kind of new to the onDraw so i don't know how to go around this problem.
I want to draw a image at this location (just so i can try the rest myself). These locations are off the ontap and * the drawables width and height. This code is in a class that extends photoview.
The problem is this resource is not being displayed, i also tried to use on a drawcirlce example that also didnt display. I have also attached that below.
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap markerBitmap = BitmapFactory.decodeResource(activity.getResources(), R.drawable.markerformap);
canvas.drawBitmap(markerBitmap, 2000, 5000, new Paint());
}
Draw circle example
canvas.drawCircle(2000, 5000,300, new Paint());
The onTouch Listener
#Override
public boolean onTouch(View v, MotionEvent event) {
if(y != event.getY() && x != event.getX()){
x = event.getX();
y = event.getY();
return true;
}
return false;
}

Related

How to check if imageview is currently zoomed or not in android?

i am zooming an image view on double tap to some certain points. on double tap event it zooms in perfectly but i want to implement the functionality of zoom out too that when user double taps the image is zoomed but when he double tap again the zoomed image must go back to original size
how this can be done? Any help will be appreciated
Here is my code
ImageView iv;
private Matrix matrix = new Matrix();
Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
getResources(), R.drawable.demoimg), size.x, size.y, true);
iv.setImageBitmap(bmp);
original_matrix = iv.getImageMatrix();
iv.setOnTouchListener(new View.OnTouchListener() {
private GestureDetector gestureDetector = new GestureDetector(SecondActivity.this, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onDoubleTap(MotionEvent e) {
float scalefactor = Math.max(1.1f, Math.min(3.0f, 4.0f));
float x = e.getX();
float y = e.getY();
matrix.setScale(scalefactor, scalefactor, x, y);
iv.setImageMatrix(matrix);
return super.onDoubleTap(e);
}
});
#Override
public boolean onTouch(View v, MotionEvent event) {
/* Toast.makeText(getApplicationContext(), "On Touch Event Called",
Toast.LENGTH_LONG).show();*/
gestureDetector.onTouchEvent(event);
return true;
}
});
The best practice is to Extend the imageView and make your customImageView.
In your customImageView keep the original size of your image and the track of image current state (zoomed in/out) then in your xml use your customImageView instead of android imageView .
You can put your zoom method in your customImageView. Then you have a customImageView which know how to zoom in, get back to the original size and its current state.

How disable scroll movement when i move a bitmap?

I want move a bitmap in the screen, but i can do it only horizontally, because if I do it vertically, the scroll of the view start and the movement of the bitmap disappears. I have
public boolean onTouchEvent(MotionEvent event)
method where in case MOVE I change the X and Y of the bitmap. Then in onDraw() method I paint the bitmap. The scroll are in xml. Inside of it are a layout and inside a View.
I would like when I touch, in case DOWN, if I touch the bitmap, disable the scroll, but I is in other place no.
that is the resume of the code
public class Table extends ScrollView{
private static Integer srcX = 0, srcY = 0;
public Table(Context context) {
super(context);
setWillNotDraw(false);
setVerticalScrollBarEnabled(true);
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for(int i=1; i<= Paint_Table.num_players; i++)
canvas.drawBitmap(bitmap, X, Y, null);
}
#SuppressLint("ClickableViewAccessibility") #Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
if (youTouchBitmap) {
srcX = x;
srcY = y;
}
break;
case MotionEvent.ACTION_MOVE:
if (srcX !=0 && srcY != 0) {
X=x;
Y=y;
invalidate();
}
break;
}
return true;
}
}
Can someone help me?
Thanks and regards.
since i dont see code ive find some link might help you:
How to disable and enable the scrolling on android ScrollView?
in the link you can see in the answers a code that is custom scroll view its work on flag.
you can choose when the scroll will work ot not.
hope i helpd :)

Pin several images at touch points - Android

Am trying to place pins wherever the user touches on an imageView. Assume a map (like Google Maps) & the user touches a point, say point A, and a pin is drawn at that point. Then, the user touches point B, then another pin (not the same previous pin relocated!) needs to be drawn at point B and so on. Right now, am able to draw a pin at the point where the user touches on the screen like this :
#Override
public void onDraw(Canvas canvas) {
....
Bitmap marker = BitmapFactory.decodeResource(getResources(),
R.drawable.icon_locationmarker);
canvas.drawBitmap(marker, mLastTouchX, mLastTouchY, null);
....
canvas.restore();
}
However, I don't want to relocate one pin across the screen wherever the user touches (which is what the above code is doing). I want to put several pins at all points wherever user touches. Am new to Android. Please help.
Eluvatar is right, you needs to create a list to store all your mark. Here's the sample of code. Remember, do add list when only motionEvent is either Action_UP or Action_DOWN only. Otherwise, there will be full of point.
public ArrayList<Coordinate> pointsList;
#Override
public void onDraw(Canvas canvas) {
....
Bitmap marker = BitmapFactory.decodeResource(getResources(),
R.drawable.icon_locationmarker);
for(Coordinate coor : pointsList){
canvas.drawBitmap(marker, coor.x, coor.y, null);
}
....
canvas.restore();
}
public View.OnTouchListener mListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
pointsList.add(new Coordinate(event.getX(), event.getY()));
}
return false;
}
};
class Coordinate{
float x;
float y;
public Coordinate(float x, float y){
this.x = x;
this.y = y;
}
}
edit: change int x,y to float x,y
you need to create a list of "touch points". then on touch add a new touch point to the list, then onDraw you iterate through that list and draw a marker on each point.
you'll also need to make sure you save the list of touch points on save instance state otherwise you'll lose them on rotate and on activity pause.

how to draw lines with drawLine method in multiple density screens

I have read many posts about my problem but I haven't found a solution. I'm going crazy!
I use the following code, in the main Class, to get the pulse on the screen:
public boolean onTouchEvent(MotionEvent event) {
x = (int) event.getX();
y = (int) event.getY();
case MotionEvent.ACTION_MOVE:
view.draw(canvas);
view.invalidate();
break;
}
view is:
DrawView view;
and DrawView is an inner class:
public class DrawView extends LinearLayout
and in the DrawView class I have the next two methods to draw the line:
public void draw(Canvas canvas) {
}
#Override
public void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
canvas.drawLine((int) 0,(int) 0,(int) x,(int) y, paint);
}
My problem is how to properly draw lines on screens with different screen densities.
I've tried with:
float d = this.getResources().getDisplayMetrics().scaledDensity;
But I still can't paint the lines correctly.
Thanks for your attention!

Zooming image in android

I want to add an image in my application which is like a road map image.
I want to add zooming features to this image as well as horizontal scrolling when the image is zoomed.
I have written some code but it allows only zooming and not scrolling.
Code:
public class Zoom extends View {
private Drawable image;
private int zoomControler=20;
public Zoom(Context context)
{
super(context);
image=context.getResources().getDrawable(R.drawable.boothmap);
setFocusable(true);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
image.setBounds((getWidth()/2)-zoomControler, (getHeight()/2)-zoomControler, (getWidth()/2)+zoomControler, (getHeight()/2)+zoomControler);
image.draw(canvas);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_DPAD_UP)// zoom in
zoomControler+=10;
if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN) // zoom out
zoomControler-=10;
if(zoomControler<10)
zoomControler=10;
invalidate();
return true;
}
}
Can you help me solving this problem. Is my code proper so far or do you have any better code?
what worked for me is : just add built in zoom controls to webview and then call its loadDataWithBaseURL method.
webview.getSettings().setBuiltInZoomControls(true);
webview.loadDataWithBaseURL("file:///android_asset/", "<img src='file:///android_res/drawable/example.png' />", "text/html", "utf-8", null);
Look at this post :
How can I get zoom functionality for images?
You have to use webview.loadUrl("file://...") to load your image
and activate zoom option
You'll get very bad results by trying to handle multi-touch with loadUrl. Try this tutorial, It provides the best source code I found to do that. If you tweak a bit the code you can make it work on 2.1 as well.
You are modifying the image bounds instead of modofying the view bounds. Try modifying the view height and width. You should be able to scroll then.
Will need to override onMeasure() method and set setMeasuredDimension as current height and width. Maintain the height and width as members.
EDIT:
class YourView extends View {
static Bitmap mOrignalBitmap = null; // check n load in constructor.
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(mWidth,mHeight);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect src = new Rect(0,0, originalWidth, originalHeight);
Rect dst = new Rect(viewLeft, viewTop, viewRight, viewBottom);
canvas.drawBitmap(mOrignalBitmap , src, dst, null);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_DPAD_UP)// zoom in
zoomControler+=10;
if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN) // zoom out
zoomControler-=10;
if(zoomControler<10)
zoomControler=10;
mWidth = //set view width with zoom
mHeight = // set view height with zoom.
requestLayout();
return true;
}
}

Categories

Resources