1.下载开源代码
1. 布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.loopj.android.image.SmartImageView
android:layout_weight="200"
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<EditText
android:id="@+id/path"
android:lines="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入浏览图片的地址"
/>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="浏览"
android:onClick="onClick"
/>
</LinearLayout>
MainActivity.java
package com.example.image;
import com.loopj.android.image.SmartImageView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
path = (EditText) findViewById(R.id.path);
path.setText("http://10.162.0.171:8080/Image/image.png");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View view) {
String imagePath = path.getText().toString().trim();
// 开源代码实现好的SmartImageView
SmartImageView imageView = (SmartImageView) findViewById(R.id.image);
// setImageUrl(String url, Integer fallbackResource, Integer
// loadingResource)
// fallbackResource:图片下载失败时显示的图片 loadingResource:图片正在下载显示的图片
imageView.setImageUrl(imagePath, R.drawable.ic_launcher,
R.drawable.ic_launcher);
}
}