Viewpage的基本使用
1. 创建pageadapter的子类
public class MyPagerAdapter extends PagerAdapter {
private ArrayList<View> mChildViews;
public MyPagerAdapter (Context context) {
mChildViews = new ArrayList<View>();
initViewButColor(context, 0xFFFF0000 );
initViewButColor(context, 0xFF00FF00 );
initViewButColor(context, 0xFF0000FF );
}
private void initViewButColor (Context context, int color) {
View view = new View(context);
LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
view.setBackgroundColor(color);
mChildViews.add(view);
}
@Override
public int getCount () {
return mChildViews.size();
}
@Override
public boolean isViewFromObject (View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public Object instantiateItem (ViewGroup container, int position) {
View view = mChildViews.get(position);
container.addView(view);
return view;
}
@Override
public void destroyItem (ViewGroup container, int position, Object object) {
View view = mChildViews.get(position);
container.removeView(view);
}
}
2. <android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
3. ViewPager vp = (ViewPager) findViewById(R.id.vp);
MyPagerAdapter adapter = new MyPagerAdapter(this );
vp.setAdapter(adapter);
基础网络编程
private void initImageViewUrl () {
try {
URL url = new URL("http://192.168.1.104/img/photo.txt" );
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
int responseCode = conn.getResponseCode();
Log.v("meeeeeee" , "获取响应码" +responseCode);
if (responseCode == 200 ) {
InputStream inputStream = conn.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
String line = null ;
while ((line = reader.readLine()) != null ) {
mImageUrlPaths.add(line);
}
}
} catch (Exception e) {
}
}
private void loadImageResource (String imageUrlPath) {
try {
URL url = new URL(imageUrlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == 200 ) {
InputStream is = conn.getInputStream();
final Bitmap bmp = BitmapFactory.decodeStream(is );
runOnUiThread(new Runnable() {
@Override
public void run () {
mCenterIv.setImageBitmap(bmp);
}
});
}
} catch (Exception e) {
}
}
FileOutputStream openFileOutput = openFileOutput(fileName,MODE_PRIVATE);
bmp.compress(CompressFormat.PNG, 50 , openFileOutput);
File file = new File(getFilesDir(),fileName);
if (file.exists()&&file.length()>0 ){
final Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
runOnUiThread(new Runnable() {
@Override
public void run () {
mCenterIv.setImageBitmap(bmp);
}
});
return ;
}
Handler的使用
可以传入一个looper对象,通过传入的looper对象可以设置它在哪个线程运行
private Handler mHandler= new Handler(){
@Override
public void handleMessage (Message msg) {
Bitmap bmp = (Bitmap) msg.obj;
mCenterIv.setImageBitmap(bmp);
}
};
功能扩展型:自定义可从网络加载的图片控件
public class MyImageView extends ImageView {
private Handler mHandler = new Handler(){
public void handleMessage (Message msg) {
switch (msg.what) {
case 1 :
Bitmap bmp = (Bitmap) msg.obj;
setImageBitmap(bmp);
break ;
case 2 :
setImageResource(R.drawable.ic_launcher);
break ;
}
}
};
public MyImageView (Context context, AttributeSet attrs) {
super (context, attrs);
}
public void setImageUrl (final String urlPath){
new Thread(){
public void run (){
try {
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
if (conn.getResponseCode() == 200 ) {
InputStream is = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
Message msg = new Message();
msg.obj = bitmap;
msg.what=1 ;
mHandler.sendMessage(msg);
} else {
Message msg = new Message();
msg.what=2 ;
mHandler.sendMessage(msg);
}
} catch (Exception e) {
Message msg = new Message();
msg.what=2 ;
mHandler.sendMessage(msg);
}
}
}.start();
}
}
第三方图片框架SmartView的使用
1. 导入
You can clone the GitHub repository: https://github.com /Section214/SmartView
Or download it directly as a ZIP file: https://github.com /Section214/SmartView/archive/master.zip
2. 在布局文件中使用
<com .loopj .android .image .SmartImageView
android:id="@+id/smiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
3. 设置图片的地址
String urlPath="http://102341/img/a.png"
smiv.setImageUrl (urlPath, R.drawable .ic _launcher, R.drawable .ic _launcher, null)