I have a View Pager to scroll the images.
Here i am using Image View to display images. I want to implement zoom in and zoom out functionality for images.
How can i implement this in android?
Try This. a Simple Way to Zoom your pictures
zooming.java
import android.app.Activity;
import android.os.Bundle;
public class zooming extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(new Zoom(this));
}
}
Zoom.java
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.View;
public class Zoom extends View {
private Drawable image;
private int zoomControler=20;
public Zoom(Context context)
{
super(context);
image=context.getResources().getDrawable(R.drawable.icon);
setFocusable(true);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//here u can control the width and height of the images........ this line is very important
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;
}
}
Hope this would help.
Related
I have a class that draws a circle at the point where the user touches. However, the circle disappears whenever a new point on the same surface is touched. I would like to keep the circle and draw a new one instead. Here is my current code.
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import java.util.Random;
public class ArtSurface extends SurfaceView implements Runnable, View.OnTouchListener {
private SurfaceHolder aHolder;
private Thread aThread;
private boolean aFlag=false;
private float aX;
private float aY;
private Paint aPaint;
private Random cRandom;
public int randomColor(){
if (cRandom==null){
cRandom=new Random();
}
int randomCol=0xff000000+256*256*cRandom.nextInt(256)+256*cRandom.nextInt(256)+cRandom.nextInt(256);
return randomCol;
}
public ArtSurface(Context context, AttributeSet attrs) {
super(context, attrs);
aHolder=getHolder();
aX=-100;
aY=-100;
aPaint=new Paint();
aPaint.setColor(randomColor());
}
public void resume(){
aThread= new Thread(this);
aFlag=true;
aThread.start();
}
public void pause(){
aFlag=false;
}
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
aX= motionEvent.getX();
aY=motionEvent.getY();
break;
case MotionEvent.ACTION_UP:
//Leave art where it is
break;
}
return true;
}
#Override
public void run() {
while(aFlag){
if(!aHolder.getSurface().isValid())
continue;
Canvas canvas=aHolder.lockCanvas();
canvas.drawARGB(255,255,255,255);
canvas.drawCircle(aX,aY,50,aPaint);
aHolder.unlockCanvasAndPost(canvas);
}
}
}```
The way the code is currently, every new touch event causes a new circle to be drawn at the new point of touch. The previous circle is removed and I can only draw one circle at a time. My main aim is to have a new circle with a random color whenever the user touches a new point on the screen. All the other previous circles should remain where they were initially rendered. Thank you.
The line
canvas.drawARGB(255,255,255,255);
keeps drawing over the rendered circle. Removing it means that each new circle is visible on the canvas.
Hello all,
in my app, can i make an character (like, cartoon character...) to animate when i touch the screen?
i have another bitmaps (static not animated) on the screen.
i have all this character (10) bitmaps in all animation position (like a cartoon)
this is the code.. ( I NEED TO ANIMATE IT FOR EACH TOUCH )
`
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
public class Show extends Activity
{
private Bitmap imageOne;
private Bitmap imageTwo;
private Bitmap imageThree;
private List<Bitmap> images;
private ExplainView myView;
private AnimationDrawable animation;
private ImageView image;`
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(0x00000400, 0x00000400);
setContentView(R.layout.frame_layout);
image = new ImageView(this);
image.setBackgroundResource(R.drawable.tom_anim);
animation = (AnimationDrawable) rocketImage.getBackground();
images = new ArrayList<Bitmap>();
praper();
myView = new ExplainView(this,images,1); <------ this is the surface view class
FrameLayout fl = (FrameLayout)findViewById(R.id.frameLayout);
fl.addView(myView);
fl.addView(image);
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
new Runnable() {
#Override
public void run() {
image.setVisibility(View.VISIBLE);
animation.stop();
animation.start();
}
};
}
return true;
}
private void praper()
{
imageOne = BitmapFactory.decodeResource(getResources(), R.drawable.trans_ar_alpha_1);
imageTwo = BitmapFactory.decodeResource(getResources(), R.drawable.trans_ar_alpha_2);
imageThree = BitmapFactory.decodeResource(getResources(), R.drawable.trans_ar_alpha_3);
images.add(0,imageOne);
images.add(1, imageTwo);
images.add(2, imageThree);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus)
{
animation.start();
}
else
{
animation.stop();
}
}
}`
of course you can simply by using
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
x = arg1.getX();
y = arg1.getY();
switch(arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
sx=arg1.getX();
sy = arg1.getY();
break;
case MotionEvent.ACTION_UP:
fx=arg1.getX();
fy = arg1.getY();
break;
}
i am new to android and stuck with getting screen size. Here is my code,
package com.piyush.droidz;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.piyush.droidz.model.boy;
public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = MainGamePanel.class.getSimpleName();
private GameThread td;
private boy b1;
private int s_width;
private int s_height;
public MainGamePanel(Context context) {
super(context);
getHolder().addCallback(this);
Log.d(TAG, "Screen width=" + s_width);
b1 = new boy(BitmapFactory.decodeResource(getResources(), R.drawable.boy), 50, 50);
td = new GameThread(getHolder(), this);
setFocusable(true);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
s_width = getHolder().getSurfaceFrame().width();
Log.d(TAG, "Present Screen width=" + s_width);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
td.setRunning(true);
td.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
td.setRunning(false);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
b1.handleActionDown((int)event.getX(), (int)event.getY());
if (event.getY()>getHeight()-50) {
td.setRunning(false);
((Activity) getContext()).finish();
}
else {
Log.d(TAG, "Coordinates: X="+event.getX()+", Y="+event.getY());
}
}
if (event.getAction()==MotionEvent.ACTION_MOVE) {
if (b1.isTouched()) {
b1.setX((int)event.getX());
b1.setY((int)event.getY());
}
}
if (event.getAction()==MotionEvent.ACTION_UP) {
b1.setTouched(false);
}
return true;
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.parseColor("#ff0000"));
b1.draw(canvas);
}
}
in the first log-tag it shows 0 where as in the second log-tag it shows exact width of emulator. I have tried initializing the "s_width" with getHolder().getSurfaceFrame().width() even before the constructor but still "s_width" is 0. Also tried WindowManagerand getwindowManager() but it is not recognized by IDE in this class. Here is my Activity class,
package com.piyush.droidz;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class DroidzActivity extends Activity {
MainGamePanel mgp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mgp = new MainGamePanel(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(mgp);
}
}
please help!
Have you tried?
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
You need to use context for getting the WindowManager. please see the documentation
http://developer.android.com/reference/android/view/WindowManager.html
Preferably the above should be done inside the constructor
public MainGamePanel(Context context) {}
Hope it helps. :)
I have two ImageViews one of them located on top of another and both inside a frameLayout. As far as I know whatever we add in frameLayout will sit on the left top part of screen and we can set their position by setting a padding for them, So I've set paddings for both of ImageViews.
upperImage = (ImageView) findViewById(R.id.imageView1);
upperImage.setPadding(250,250, 250, 0);
lowerImage = (ImageView) findViewById(R.id.imageView2);
lowerImage.setPadding(250, 361, 250, 0); //250+Height of Image
I want to rotate my ImageViews and I'm almost done with that. the only thing I still haven't done is the location of pivot.
I want to set the pivot point on the center bottom of my ImageViews. I've tried many numbers but I didn't get the answer and didn't even find out how that method works. I mean I don't know what's the number it gets as an argument. Is that pixels or what?(I call the method for animations in onCreate method so I can't use getWidth() and getHeight() methods so I enter the numbers that these methods return myself.)
I have even tried different places to set pivot point in java and even XML layout. But that didn't make any changes.
All I want to know is how to set the pivot point at center bottom of my ImageView using setPivot methods?(They don't seem to be working methods) Can it be the problem with setting the point in onCreate method? If yes how can I make it work in onCreate?
Remember to put the actual graphic in the res folders for your bitmap and change the name in the constructor or whatever when it loads the bitmap
PivotView.java
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class PivotView extends View{
public Bitmap arm;
public Bitmap hand;
public int armrotation = 0;
public int handrotation = 0;
public PivotView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
arm = BitmapFactory.decodeResource(getResources(),R.drawable.arm );
hand = BitmapFactory.decodeResource(getResources(),R.drawable.hand );
}
#Override
protected void onDraw (Canvas canvas){
canvas.rotate((float)armrotation, 0, (float)canvas.getHeight());
canvas.drawBitmap(arm, new Rect(0,0,arm.getWidth(),arm.getHeight()), new Rect(0,
canvas.getHeight()*1/3,canvas.getWidth()*1/5,canvas.getHeight()), null);
Matrix matrix = new Matrix();
matrix.setRectToRect(new RectF(0,0,(float)hand.getWidth(),(float)hand.getHeight()), new RectF(0,
(float)canvas.getHeight()*1/4,(float)canvas.getWidth()*1/5,(float)canvas.getHeight()*4/10), Matrix.ScaleToFit.FILL);
matrix.preRotate((float)handrotation,(float)hand.getWidth()/2,(float)hand.getHeight());
canvas.drawBitmap(hand, matrix, null);
}
public void rotatArm(int rotate){
Log.d("arm", String.valueOf(rotate));
armrotation = rotate;
}
public void rotateHand(int rotate){
Log.d("hand", String.valueOf(rotate));
handrotation = rotate;
}
}
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener{
public SeekBar hand;
public SeekBar arm;
public PivotView robot;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hand = (SeekBar)findViewById(R.id.seekhand);
hand.setMax(360);
arm = (SeekBar)findViewById(R.id.seekarm);
arm.setMax(90);
robot = (PivotView)findViewById(R.id.pivot);
hand.setOnSeekBarChangeListener(this);
arm.setOnSeekBarChangeListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
switch(seekBar.getId()){
case R.id.seekarm:
rotatethearm(progress);
//robot.invalidate();
break;
case R.id.seekhand:
rotatethehand(progress);
//robot.invalidate();
break;
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void rotatethearm(int progress){
robot.rotatArm(progress);
robot.invalidate();
}
public void rotatethehand(int progress){
robot.rotateHand(progress);
robot.invalidate();
}
}
Make your layout like so with custom view, remember to use your package name in place where it says com.example.pivotview in the layout for the custom view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.example.pivotview.PivotView
android:id="#+id/pivot"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.example.pivotview.PivotView>
<SeekBar
android:id="#+id/seekarm"
android:layout_alignParentBottom = "true"
android:layout_width = "match_parent"
android:layout_height="wrap_content"/>
<SeekBar
android:id="#+id/seekhand"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/seekarm"
/>
</RelativeLayout>
I have to achieve that the Touch Scroll on the ViewFlipper. For Example. I have two Images. At First, ViewFlipper shows an First Image. Now I Flung the view from right to left. The First Image view Slide out left and the Second Slide in from Left. I can achieve it By this Post. But I want to Scroll the image. That is, on the Action_Move Event I want to do Touch Scroll. For Example, when I move the touch from right to left it will flung how much the touch moves. on that time the output should show both images partly.
How to do that? What I have to measure the Screen levels(height & width). Example codes are more helpful.
If you need to detect scroll on only viewflipper which is not occupying entire screen, then try the below
gestureDetector = new GestureDetector(new MyGestureDetector());
viewFlipper.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return false;
}
return true;
}
});
and MyGestureDetector will be same as in http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/
package com.appaapps.flipper;
import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ViewFlipper;
//------------------------------------------------------------------------------
// Flipper - Philip R Brenan at gmail.com
//------------------------------------------------------------------------------
public class FlipperActivity extends Activity {
ViewFlipper f;
DrawView a, b, c;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
f = new ViewFlipper(this);
a = new DrawView(this, "aaaaa");
b = new DrawView(this, "BBBBB");
c = new DrawView(this, "ccccc");
f.addView(a);
f.addView(b);
f.addView(c);
setContentView(f);
}
//------------------------------------------------------------------------------
// Draw
//------------------------------------------------------------------------------
class DrawView extends View implements View.OnTouchListener {
final String text;
DrawView(Context Context, String Text) {
super(Context);
text = Text;
setOnTouchListener(this);
}
public void onDraw(Canvas Canvas) {
super.onDraw(Canvas);
Paint p = new Paint();
p.setColor(0xffffffff);
p.setTextSize(20);
Canvas.drawText(text, 0, 20, p);
}
public boolean onTouch(View v, MotionEvent event) {
final int a = event.getAction();
if (a == MotionEvent.ACTION_DOWN) {
final int i = f.getDisplayedChild(), n = f.getChildCount();
f.setDisplayedChild((i + 1) % n);
}
return true;
}
}
}