如何转动静态图像
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.67"
android:src="@drawable/static_image" />
进入一个ImageView,其源可以动态设置为不在res文件夹中的数据吗?
也就是说,我的应用程序在屏幕上有一个图标,但图标的实际图像是从外部服务器下载的,可以动态更改。如何在下载时使用所需图像更新ImageView?我想要一些功能上的东西:
Image selectedImage = //get from server
myImageView.setImage(selectedImage);
答案 0 :(得分:2)
你的问题不明确。如果您只想将图像(在某个网址中)设置为图像视图,
Bitmap bmp=getBitmapFromURL(ur url here);
imgview.setImageBitmap(bmp);
并编写此函数:
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap mybitmap = BitmapFactory.decodeStream(input);
return mybitmap;
} catch (Exception ex) {
return null;
}
答案 1 :(得分:1)
yourImageView.setImageBitmap(bitmap);
从服务器获取位图:
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
答案 2 :(得分:1)
据我了解你的问题,
ImageView selectedImage;
selectedImage = (ImageView)findViewById(R.id.imageView1);
Bitmap bmImg;
downloadFile(imageUrl);
这是downloadFile()
方法......
void downloadFile(String fileUrl){
URL myFileUrl =null;
try {
myFileUrl= new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
selectedImage.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 3 :(得分:0)
您需要创建自定义视图并覆盖onDraw()。从那里你可以获得绘图画布,你可以做任何你喜欢的事情。因此,假设您的动态图像可以转换为位图,您可以在Canvas中使用许多drawBitmap()方法。
请注意,您还必须覆盖onMeasure(),它允许您的视图告诉布局管理器您需要多少空间。
这里有很多好消息: http://developer.android.com/guide/topics/ui/custom-components.html