我正在使用此代码,但我遇到了这个问题:我的图片不是静态的,在drawable文件夹上,我从网址获取它。
我必须做出哪些改变?下面是我获取图像和缩放的代码。
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;
}
}
和我的代码,以获得正常工作的图像。
try {
ImageView i = (ImageView)findViewById(R.id.icon);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}