Android基础_Viewpage/网络编程/Handler/扩展型自定义控件/SmartView(五)

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);
    }
    //添加一个只有背景颜色的view到mChildViews中
    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();
    }
    //通过该方法来确定该展示哪个view,这里的view是instantiateItem()返回的
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }
    //实例化就是将view添加到container中
    @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);

基础网络编程

//1.从网络中下载存有图片地址的txt文件,从流中读取每行的图片地址并写入一个list中
    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) {
        }
    }

    //2.从网络加载图片到本地
    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{
    /*创建一个Handler对象
     * msg.what msg的flags
     * */
    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);
    }
    /*需求:存进一个Url对象就会展示图片
     * 1.创建一个方法带有url的形参
     * 2.创建一个子线程
     * 3.进行一个网络请求
     * */
    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);
                        // setImageBitmap(bitmap);

                        Message msg = new Message();
                        msg.obj = bitmap;
                        msg.what=1;
                        mHandler.sendMessage(msg);
                    } else {
                        // setImageResource(R.drawable.ic_launcher);
                        Message msg = new Message();
                        msg.what=2;
                        mHandler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    // setImageResource(R.drawable.ic_launcher);
                    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);        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值