您的位置:首页 > 移动开发 > Android开发

Android中下载图片展示到界面点击放大

2014-11-29 10:20 162 查看
项目结构:



activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<ImageView

android:id="@+id/imageView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/ic_launcher"

android:layout_gravity="center"

/>

</LinearLayout>

dialog_photo_entry.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/LinearLayout1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical" >

<ImageView

android:id="@+id/large_image "

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:scaleType="fitXY"

>

</ImageView>

</LinearLayout>

MainActivity

public class MainActivity extends Activity {

private ImageView im;

InputStream is =null;

Bitmap bitmap =null;

//一张图片的路径,直接在浏览器就可以访问到该图片

String path ="http://a.hiphotos.baidu.com/pic/w%3D230/sign=bf59456cc9fcc3ceb4c0ce30a244d6b7/4afbfbedab64034f80b90b48aec379310a551d0c.jpg";

Handler handler=new Handler(){

public void handleMessage(android.os.Message msg) {

switch (msg.what) {

case 1:

try {

im.setImageBitmap(bitmap);

} catch (Exception e) {

e.printStackTrace();

}finally{

if (is!=null) {

try {

is.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

break;

}

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

im = (ImageView) findViewById(R.id.imageView1);

im.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

getBigPicture(bitmap);

}

});

//开子线程,下载图片

new Thread(new Runnable() {

@Override

public void run() {

try {

//连接网络6句代码

URL url = new URL(path);

HttpURLConnection connection=(HttpURLConnection) url.openConnection();

connection.setConnectTimeout(5000);

connection.setRequestMethod("GET");

if (connection.getResponseCode()==200) {

System.out.println("连接成功》》》》》》》》》》》》");

// 得到和图片连接的流

is = connection.getInputStream();

bitmap = BitmapFactory.decodeStream(is);

//发送消息

Message msg=Message.obtain();

msg.what=1;

handler.sendMessage(msg);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}).start();

}

/**

* 点击图片放大查看

*

*/

private void getBigPicture(Bitmap b) {

LayoutInflater inflater = LayoutInflater.from(this);

View imgEntryView = inflater.inflate(R.layout.dialog_photo_entry, null); // 加载自定义的布局文件

final AlertDialog dialog = new AlertDialog.Builder(this).create();

ImageView img = (ImageView) imgEntryView.findViewById(R.id.large_image);

if (b != null) {

Display display = MainActivity.this.getWindowManager()

.getDefaultDisplay();

int scaleWidth = display.getWidth();

int height = b.getHeight();// 图片的真实高度

int width = b.getWidth();// 图片的真实宽度

LayoutParams lp = (LayoutParams) img.getLayoutParams();

lp.width = scaleWidth;// 调整宽度

lp.height = (height * scaleWidth) / width;// 调整高度

img.setLayoutParams(lp);

img.setImageBitmap(b);

dialog.setView(imgEntryView); // 自定义dialog

dialog.show();

}

// 点击布局文件(也可以理解为点击大图)后关闭dialog,这里的dialog不需要按钮

imgEntryView.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View paramView) {

if (dialog.isShowing()) {

dialog.cancel();

}

}

});

}

}//class

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>

如果没有明白下载我的例子:

http://download.csdn.net/detail/zhaihaohao1/8389783
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: