Andoid实战 - 个人App 乐逗项目 (引导页优化,一些错误)

1.回顾

   这篇文章呢,本来是昨天应该写呢,因为追剧了闭嘴,延迟到了今天;昨天找了找,数据来源和wordpress JSON API 有关的插件,还不错,提供了文章的API操作和用户的API操作,还没使用过,这里就不做过多的评价。

  (还是使用习惯的HTML编辑器写吧,MarkDown 还不习惯排版)

2.实现

   (1)引导页优化;

   (2)使用Android Studio 时,两个错误;

   (3)Design Support Library22.2 学习 

3.引导页优化

   3.1效果图


      

     3.2 优化

          昨天在真机(小米4)上测试了下,以失败而告终,Android Stuido 也不打印logcat .这是第一次遇到这种情况,后在手机开发者选项里,将程序崩溃的弹窗打开了.
小米系统这还不错,自己输出了log , 看后,发现是内存溢出问题; 这就很明显了,目前是静态的,所以图片加载是 drawable 下的, 图片也不大55k左右,加载到内存中占得大了.后开始优化并添加了切换效果:
    优化:
   (1).ViewpagerAdapter 独立实现;

              将pagerAdapter 单独分离出来,将图片id 作为参数,在适配的时候,才去加载 图片资源,并通过 findviewbytag() , 获得对应的imageview 进行操作;

package labelnet.cn.ledou.adpater;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import labelnet.cn.ledou.util.PictureUtil;

public class ViewPagerAdapter extends PagerAdapter {
    /**
     * Welcome引导页适配器
     * ViewPager的适配器
     */
        private int [] ids;
        private Context context;

    /**
     * 构造函数
     * @param ids
     * @param context
     */
        public ViewPagerAdapter(int [] ids,Context context){
            this.ids=ids;
            this.context=context;
        }

        @Override
        public int getCount() {
            return ids.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }


        /**
         * 重写 instantiateItem 方法
         *
         * @param container
         * @param position
         * @return
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            Bitmap bitmap=PictureUtil.readBitMap(context, ids[position]);

            ImageView imageView=new ImageView(context);
            imageView.setTag(ids[position]);
            imageView.setImageBitmap(bitmap);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            container.addView(imageView);

            return imageView;
        }

        /**
         * 重写 destroyItem 方法
         *
         * @param container
         * @param position
         * @param object
         */

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //通过tag 移除 对应的ImageView
            ImageView imageView=(ImageView)container.findViewWithTag(ids[position]);
            container.removeView(imageView);
        }
    }



   (2).Viewpager 切换动画实现;

          动画的实现是通过官方提供的切换效果来实现,这里参考我的文章:Viewpager动画实现 

          我这里实现的缩放的动画,实现类为:

package labelnet.cn.ledou.anim;

import android.annotation.SuppressLint;
import android.support.v4.view.ViewPager;
import android.view.View;

public class DepthPageTransformer implements ViewPager.PageTransformer {

    /**
     * Viewpager 切换动画实现;
     */
    private static final float MIN_SCALE = 0.75f;
    
    @SuppressLint("NewApi")
	public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();

        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.setAlpha(0);

        } else if (position <= 0) { // [-1,0]
            // Use the default slide transition when moving to the left page
            view.setAlpha(1);
            view.setTranslationX(0);
            view.setScaleX(1);
            view.setScaleY(1);

        } else if (position <= 1) { // (0,1]
            // Fade the page out.
            view.setAlpha(1 - position);

            // Counteract the default slide transition
            view.setTranslationX(pageWidth * -position);

            // Scale the page down (between MIN_SCALE and 1)
            float scaleFactor = MIN_SCALE
                    + (1 - MIN_SCALE) * (1 - Math.abs(position));
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

        } else { // (1,+Infinity]
            // This page is way off-screen to the right.
            view.setAlpha(0);
        }
    }

}

      调用实现:

      vp_welcome = (ViewPager) findViewById(R.id.vp_welcome);

        //设置适配器
        ViewPagerAdapter pagerAdapter=new ViewPagerAdapter(imagesids,this);
        //设置 切换动画
        vp_welcome.setPageTransformer(true, new DepthPageTransformer());

        vp_welcome.setAdapter(pagerAdapter);


   (3).解决Viewpager内存溢出问题,通过Pictureutil实现对图片进行压缩实现;

           这里的实现通过 质量压缩实现 ,实现方法为(前面已经封装有单独的实现类):

/**
     * 以最省内存的方式读取本地资源的图片
     * @param context
     * @param resId
     * @return
     */
    public static Bitmap readBitMap(Context context, int resId){
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        //获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        return compressImage(BitmapFactory.decodeStream(is,null,opt));
    }


    /**
     * 1.质量压缩
     *
     * @param image
     * @return
     */
    public static Bitmap compressImage(Bitmap image) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
        int options = 100;
        while (baos.toByteArray().length / 1024 > 100) {    //循环判断如果压缩后图片是否大于100kb,大于继续压缩
            baos.reset();//重置baos即清空baos
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
            options -= 10;//每次都减少10
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
        return bitmap;
    }


4.两个错误

   在使用Android Studio的时候,配置Gradle,加载 兼容包的时候,出现了两处错误;

     4.1.Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/local/jdk1.7.0_79/bin/java'' finished with non-zero exit value 2
 
原因:因为重复引用了jar包,在Android Studio 中注释 compile fileTree(dir: 'libs', include: ['*.jar']) 即可; 如果需要使用libs下的 可以单独引入;


    4.2.Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Inverse'. Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Button.Colored'.


原因:自己的工程sdk版本低出错; 解决,将引入的改为 sdK 版本改为 22 ,和 引入的   compile 'com.android.support:design:22.2.1'
    compile 'com.android.support:appcompat-v7:22.2.1' , 不要 23 的;目前 22的就该使用了;
http://blog.csdn.net/linshijun33/article/details/48254583 希望可以提供帮助


5.结语

       因为在Android Stuido 上开发,所以难免遇到各种困难;这里我说说在使用兼容包的时候的一些事;在使用v7兼容包和design 兼容控件的时候,需要在 build.gradle 中配置,后做同步操作,这是需要注意应用的当前sdk的版本,最后和你的sdk版本一样的区间,比如我的 sdk为22 ,一定要配置为 23以下的兼容包 ; 还有最主要的是,现在(2015-10-21) appcompat-v7:23 以至于大于23的最好不要使用,不然频频出错,特别烦人;建议使用小的版本实现。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值