放大和缩小在android中的viewpager

时间:2012-09-19 09:22:25

标签: android android-viewpager

我有一个View Pager来滚动图像 在这里,我使用图像视图来显示图像。我想为图像实现放大和缩小功能 我怎么能在android中实现这个?

1 个答案:

答案 0 :(得分:0)

试试这个。一种放大图片的简单方法

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;
        }
}

希望这会有所帮助。