ImageView实现图像旋转
效果:
代码:

1 package com.leaf.android; 2 3 import android.app.Activity; 4 import android.graphics.Bitmap; 5 import android.graphics.Matrix; 6 import android.graphics.drawable.BitmapDrawable; 7 import android.os.Bundle; 8 import android.util.DisplayMetrics; 9 import android.widget.ImageView; 10 import android.widget.LinearLayout; 11 import android.widget.SeekBar; 12 import android.widget.SeekBar.OnSeekBarChangeListener; 13 import android.widget.TextView; 14 15 public class Main extends Activity implements OnSeekBarChangeListener { 16 /** Called when the activity is first created. */ 17 18 private int minWidth = 80; 19 private ImageView imageView; 20 private TextView textView1, textView2; 21 private Matrix matrix = new Matrix(); 22 23 @Override 24 public void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.main); 27 imageView = (ImageView) this.findViewById(R.id.imageview); 28 SeekBar seekBar1 = (SeekBar) this.findViewById(R.id.seekbar1); 29 SeekBar seekBar2 = (SeekBar) this.findViewById(R.id.seekbar2); 30 textView1 = (TextView) this.findViewById(R.id.textview1); 31 textView2 = (TextView) this.findViewById(R.id.textview2); 32 seekBar1.setOnSeekBarChangeListener(this); 33 seekBar2.setOnSeekBarChangeListener(this); 34 35 DisplayMetrics dm = new DisplayMetrics(); 36 getWindowManager().getDefaultDisplay().getMetrics(dm); 37 seekBar1.setMax(dm.widthPixels - minWidth); 38 } 39 40 public void onProgressChanged(SeekBar seekBar, int progress, 41 boolean fromUser) { 42 // TODO Auto-generated method stub 43 if (seekBar.getId() == R.id.seekbar1) { 44 int newWidth = progress + minWidth; 45 int newHeight = (int) (newWidth * 3 / 4);// 按照原图像进行缩放的功能 46 imageView.setLayoutParams(new LinearLayout.LayoutParams(newWidth, 47 newHeight)); 48 textView1.setText("图像宽度:" + newWidth + "图像宽度:" + newHeight); 49 } else if (seekBar.getId() == R.id.seekbar2) { 50 Bitmap bitmap = ((BitmapDrawable) (getResources() 51 .getDrawable(R.drawable.dog))).getBitmap(); 52 matrix.setRotate(progress); 53 bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), 54 bitmap.getHeight(), matrix, true); 55 imageView.setImageBitmap(bitmap); 56 textView2.setText(progress + "度"); 57 } 58 } 59 60 public void onStartTrackingTouch(SeekBar seekBar) { 61 // TODO Auto-generated method stub 62 63 } 64 65 public void onStopTrackingTouch(SeekBar seekBar) { 66 // TODO Auto-generated method stub 67 68 } 69 }

1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <ImageView 8 android:id="@+id/imageview" 9 android:layout_width="200dp" 10 android:layout_height="150dp" 11 android:scaleType="fitCenter" 12 android:src="@drawable/dog" /> 13 14 <TextView 15 android:id="@+id/textview1" 16 android:layout_width="fill_parent" 17 android:layout_height="wrap_content" 18 android:layout_marginTop="10dp" 19 android:text="图像宽度:240 图像高度:160" /> 20 21 <SeekBar 22 android:id="@+id/seekbar1" 23 android:layout_width="200dp" 24 android:layout_height="wrap_content" 25 android:layout_marginTop="10dp" 26 android:max="240" 27 android:progress="120" /> 28 29 <TextView 30 android:id="@+id/textview2" 31 android:layout_width="fill_parent" 32 android:layout_height="wrap_content" 33 android:layout_marginTop="10dp" 34 android:text="0度" /> 35 36 <SeekBar 37 android:id="@+id/seekbar2" 38 android:layout_width="200dp" 39 android:layout_height="wrap_content" 40 android:layout_marginTop="10dp" 41 android:max="360" /> 42 43 </LinearLayout>
ImageView获取网络图片
Android平台有3种网络接口可以使用,它们分别是:java.net.*(标准java接口)、org.apache(Apache接口)和android.net.*(Android网络接口)。本文将使用java.net.*(标准java接口)来实现获取一张网络图片,并将其显示在ImageView控件中。
HttpURLConnection的常用方法
在Android的应用开发中,HttpURLConnection的常用方法有以下一些:
abstract void discontent(); //关闭此链接
String getContentEncoding(); //获取传输响应体的编码
InputStream getErrorStream(); //获取从服务器返回的错误流
String getRequestMethod(); //获取请求远程HTTP服务器的请求方法
int getResponseCode(); //获取响应远程HTTP服务器的响应值
String getResponseMessage(); //获取响应远程HTTP服务器的响应消息
void setRequestMethod(String mothod); //设置请求远程HTTP服务器的请求方法
几个常用的服务器响应值
当我们连接了某一服务器后,需要使用getResponseCode()方法来获取服务器的响应值,并以此来判断网络连接是否正常。常用的几个服务器响应值如下:
HTTP_BAD_GATEWAY //502网关错误
HTTP_BAD_REQUEST //400请求错误
HTTP_CLIENT_TIMEOUT //408客户端超时
HTTP_NOT_FOUND //404未找到服务器
HTTP_OK //200正常连接
HTTP_UNAVAILABLE //503网络不可用
实例
要将一张网络图片显示在ImageView控件中,首先需要从网络上获取该图片资源,下面的代码实现了这一功能,并将获取的图片资源以流的形式返回。
/** * 从网络中获取图片,以流的形式返回 * @return */ public static InputStream getImageViewInputStream() throws IOException { InputStream inputStream = null; URL url = new URL(URL_PATH); //服务器地址 if (url != null) { //打开连接 HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); httpURLConnection.setConnectTimeout(3000);//设置网络连接超时的时间为3秒 httpURLConnection.setRequestMethod("GET"); //设置请求方法为GET httpURLConnection.setDoInput(true); //打开输入流 int responseCode = httpURLConnection.getResponseCode(); // 获取服务器响应值 if (responseCode == HttpURLConnection.HTTP_OK) { //正常连接 inputStream = httpURLConnection.getInputStream(); //获取输入流 } } return inputStream; }
对以上代码,有以下几点需要说明:
(1)由于是连接网络,不免会出现一些异常情况,所以在代码中使用了throws IOException来抛出异常。在调用该函数时就可以使用try-catch来捕获异常,并针对异常情况做对应的处理。
(2)语句URL url = new URL(URL_PATH);指定了服务器的路径,也就是我们要访问的网络图片的地址。那么如何将一张图片发布到服务器上呢?这里,我们可以通过使用MyEclipse和Tomcat来完成服务器的构建(构建方法可以参考《Android学习笔记20:Http协议及Java Web编程》http://www.cnblogs.com/menlsh/archive/2013/01/27/2878638.html)。构建好了该服务器之后,只需要将该图片放到工程的WebRoot目录下,然后启动该工程,我们就可以通过URL_PATH路径访问到服务器上的图片了。
(3)因为在该工程中使用到了网络资源,所以我们还需要在AndroidManifest.xml中打开网络的访问权限,实现的方法很简单,在AndroidManifest.xml文件中加入以下一段代码即可。
<!-- 添加网络授权 --> <uses-permission android:name="android.permission.INTERNET" > </uses-permission>
至此,我们完成了从服务器上以流的形式获取一张图片资源,那么如何利用该图片资源流来创建一张Bitmap图片呢?这里,我们可以使用BitmapFactory对象的Static Bitmap decodeStream(InputStream inputStream)方法依据输入流来创建一张Bitmap图片,然后通过使用ImageView的setImageBitmap(Bitmap bitmap)方法将该Bitmap图片添加到ImageView控件中进行显示,具体的实现代码如下:
try { InputStream inputStream = HttpUtils.getImageViewInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); mImageView.setImageBitmap(bitmap); } catch (IOException e) { }
代码:

1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <ImageView 8 android:id="@+id/imageview" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" /> 11 12 <Button 13 android:id="@+id/button" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:text="下载网络图片" /> 17 18 </LinearLayout>

1 package com.leaf.android; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 6 import android.app.Activity; 7 import android.graphics.Bitmap; 8 import android.graphics.BitmapFactory; 9 import android.os.Bundle; 10 import android.view.View; 11 import android.widget.Button; 12 import android.widget.ImageView; 13 14 public class Main extends Activity { 15 /** Called when the activity is first created. */ 16 17 private Button button; 18 private ImageView imageView; 19 20 @Override 21 public void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.main); 24 button = (Button) this.findViewById(R.id.button); 25 imageView = (ImageView) this.findViewById(R.id.imageview); 26 button.setOnClickListener(new View.OnClickListener() { 27 28 public void onClick(View v) { 29 // TODO Auto-generated method stub 30 try { 31 InputStream inputStream = HttpUtils 32 .getImageViewInputStream(); 33 Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 34 imageView.setImageBitmap(bitmap); 35 } catch (IOException e) { 36 // TODO: handle exception 37 } 38 // imageView.setImageBitmap() 39 } 40 }); 41 } 42 }

1 package com.leaf.android; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.net.HttpURLConnection; 6 import java.net.URL; 7 8 public class HttpUtils { 9 10 private final static String URL_PATH = " ";// 访问网络图片的路径 11 12 public HttpUtils() { 13 14 } 15 16 // 从网络中获得图片信息,以流的形式返回 17 public static InputStream getImageViewInputStream() throws IOException{ 18 InputStream inputStream = null; 19 URL url = new URL(URL_PATH); 20 if (url != null) { 21 HttpURLConnection httpURLConnection = (HttpURLConnection) url 22 .openConnection(); 23 httpURLConnection.setConnectTimeout(3000); 24 httpURLConnection.setRequestMethod("GET"); 25 httpURLConnection.setDoInput(true); 26 int response_code = httpURLConnection.getResponseCode(); 27 if (response_code == 200) { 28 inputStream = httpURLConnection.getInputStream(); 29 } 30 } 31 return inputStream; 32 } 33 }
参考文献:http://www.cnblogs.com/menlsh/archive/2013/02/01/2889619.html