Android中下载图片展示到界面点击放大
2015-01-24 12:27
351 查看
项目结构:
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
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
相关文章推荐
- Android中下载图片展示到界面点击放大
- android 图片点击一下就放大到全屏,再点一下就回到原界面
- android 图片点击一下就放大到全屏,再点一下就回到原界面
- android 图片点击一下就放大到全屏,再点一下就回到原界面
- ANDROID展示HTML图文混排--实现图片缩小并点击放大
- Android 点击图片放大展示 展示中可调节图片的缩放显示 图片查看器
- Android中照相,从相册选取照片,压缩,保存到手机内存,展示到界面,点击放大,上传。(含demo)
- android 图片点击一下就放大到全屏,再点一下就回到原界面
- Android中照相,从相册选取照片,压缩,保存到手机内存,展示到界面,点击放大,上传。(含demo)
- android 图片下载、放大缩小
- android bundle 在activity之间传递数据 点击一组图片放大,再次点击恢复
- Android浏览图片,点击放大至全屏效果
- 可拖动FLASH+XML图片展示效果【点击放大带标题内容】
- Android有效的展示图片 (一) Loading Large Bitmaps Efficiently 有效的下载大图片
- android点击实现图片放大缩小
- android-高仿手机淘宝--商铺界面产品展示及图片缩放
- android图片点击放大动画,并遮挡旁边的控件
- Android浏览图片,点击放大至全屏效果
- android 3d 图片浏览 点击放大
- 【Android 界面效果30】Android中ImageSwitcher结合Gallery展示SD卡中的资源图片