2010-10-22 83 views
5

我有一個在main.XML中聲明的ImageView。我希望它被放大。 這怎麼可以在Android中完成?如何在Android中縮放ImageView

+0

http://stackoverflow.com/questions/1939103/how-to-make-my-imageview-zoomable,http://stackoverflow.com/questions/1939103/how - 我的imageview-zoomable – ArK 2010-10-22 10:15:08

回答

1

得到它here

public class Zoom extends View { 
    private Drawable image; 
    private int zoomControler=200; 
    public Zoom(Context context) 
    { 
    super(context); 
    image=context.getResources().getDrawable(R.drawable.gallery_photo_1); 
    setFocusable(true); 
    } 
    @Override 
    protected void onDraw(Canvas canvas) { 
    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; 
    } 
    } 
+0

謝謝,其工作正常 – 2010-10-22 10:34:33

+2

但我有一個佈局中的圖像,我將使用findViewById()獲取該ImageView的引用,我想動態縮放它。假設它是垂直的LinearLayout,我在這個ImageView下面有一些其他的小部件。如何在Activity中動態縮放? – 2010-10-22 10:58:38