• Android WelcomeActivity 启动画更换网络图片


    1、运行效果 

    第一张是本地的启动图,第二张是网络启动图

         

    2、用到的第三方jar包   Android-Universal-Image-Loader-master

        不熟的请看  Android-Universal-Image-Loader 框架使用

    3、主要方法

    (1)MyApplication

    package com.app.android01 ;
    import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
    import com.nostra13.universalimageloader.core.DisplayImageOptions;
    import com.nostra13.universalimageloader.core.ImageLoader;
    import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
    import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
    import android.app.Application;
    import android.content.Context;
    import android.graphics.Bitmap.Config;
    
    public class MyApplication extends Application {
    
        public static DisplayImageOptions options; 
    
        @Override
        public void onCreate() {
            super.onCreate();
            initImageLoader( getApplicationContext() );
        }
    
        private void initImageLoader(Context context) {
            ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
            .threadPriority(Thread.NORM_PRIORITY - 2)
            .denyCacheImageMultipleSizesInMemory()
            .diskCacheExtraOptions(720, 1280, null)
            .diskCacheFileNameGenerator(new Md5FileNameGenerator())
            .diskCacheSize(50 * 1024 * 1024) // 50 Mb
            .tasksProcessingOrder(QueueProcessingType.LIFO)
            .diskCacheFileCount( 500 )
            .build();
            ImageLoader.getInstance().init( config );
    
            initOptions() ;
        }
    
        private void initOptions(){
            options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)  //启用内存缓存
            .cacheOnDisk(true)    //启用磁盘缓存
            .considerExifParams(true)
            .bitmapConfig(Config.RGB_565 )
            .build();
        }
    }

    (2)WelcomeActivity

    package com.app.android01 ;
    import com.nostra13.universalimageloader.core.ImageLoader;
    import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.view.View;
    import android.widget.ImageView;
    
    public class WelcomeActivity extends Activity {
        
        private boolean tag = true ;
        //总时间
        private int totalTime = 0 ; 
    
        //网络启动画
        private String imageString = "http://c.hiphotos.baidu.com/image/pic/item/8435e5dde71190efec7149d6cc1b9d16fdfa6035.jpg" ;
    
        private ImageView  welcome_image  ; 
    
        /**
         * Handler:跳转到不同界面
         */
        private Handler mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case 1 :
                    totalTime = totalTime + 200 ;
    
                    if( totalTime == 3000 ){  //如果总时间大于3秒,就跳转到MainActivity,并且结束计数线程
                        tag = false ;
                        startActivity( new Intent( WelcomeActivity.this , MainActivity.class ));
                        finish() ;
                    }
                    break ;
                }
                super.handleMessage(msg);
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView( R.layout.activity_welcome ) ;
            welcome_image =  (ImageView) findViewById( R.id.welcome_image ) ;
    
            //启动记时工具
            new Thread(new ThreadShow()).start() ;
    
            ImageLoader.getInstance().loadImage( imageString , MyApplication.options , new SimpleImageLoadingListener(){
                @SuppressLint("NewApi") @Override
                public void onLoadingComplete(String imageUri, View view,
                        Bitmap loadedImage) {
                    if( totalTime <= 800 ){   //如果在800毫秒内把网络图片加载出来就显示,否则就只显示本地的
                        Drawable drawable = new BitmapDrawable( WelcomeActivity.this.getResources(), loadedImage );
                        welcome_image.setBackground( drawable ) ;
                    }
                }
            });
        }
    
        /**
         * 线程类
         * @author admin
         * 计时器
         */
        class ThreadShow implements Runnable {
            @Override
            public void run() {
                while ( tag ) {
                    try {
                        Thread.sleep( 200 ); //每间隔200毫秒发送一次
                        Message msg = new Message();
                        msg.what = 1 ;
                        mHandler.sendMessage(msg);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    (3)MainActivity

    package com.app.android01 ;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
        private TextView textView ; 
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView( R.layout.activity_main ) ;
    
            textView = (TextView) findViewById( R.id.tv ) ;
        }
    }

    4、所需要的权限

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

    5、项目下载地址

    http://download.csdn.net/detail/yanzi2015/8890461

  • 相关阅读:
    java中的String.format使用
    白话解析平安笔试题:多线程交替打印
    centos7 yum install redis
    CentOS7 linux下yum安装redis以及使用
    开源规则流引擎实践
    java中System.err.print和System.out.print区别
    drools -规则语法
    小明历险记:规则引擎drools教程一
    规则引擎drools封装
    C#中Encoding.Unicode与Encoding.UTF8的区别
  • 原文地址:https://www.cnblogs.com/zhaoyanjun/p/4639180.html
Copyright © 2020-2023  润新知