那些用过的code

mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
ViewPager切换动画之ZoomOutPageTransformer
====

  /**
  * @author xuchen
  * on 2017/1/4.
  */
  @SuppressLint("NewApi")
   public class ZoomOutPageTransformer implements PageTransformer {
         private static float MIN_SCALE = 0.70f;

         private static float MIN_ALPHA = 0.2f;

         @Override
          public void transformPage(View view, float position) {
             int pageWidth = view.getWidth();
             int pageHeight = view.getHeight();

             if (position < -1) {
                // This page is way off-screen to the left.
                 view.setAlpha(0);
                   } else if (position <= 1) { // [-1,1]
                      // Modify the default slide transition to
                      // shrink the page as well
                      float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
                      float vertMargin = pageHeight * (1 - scaleFactor) / 2;
                      float horzMargin = pageWidth * (1 - scaleFactor) / 2;
                             if (position < 0) {
                                  view.setTranslationX(horzMargin - vertMargin / 2);
                                        } else {
                                       view.setTranslationX(-horzMargin + vertMargin / 2);
        }
               // Scale the page down (between MIN_SCALE and 1)
              view.setScaleX(scaleFactor);
              view.setScaleY(scaleFactor);
            // Fade the page relative to its size.
              view.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE)
                / (1 - MIN_SCALE) * (1 - MIN_ALPHA));
                    } else { // (1,+Infinity]
                    // This page is way off-screen to the right.
                   view.setAlpha(0);
         }
      }
   }

ViewPager切换动画之DepthPageTransformer

/**
 * @author xuchen
 * on 2017/1/4.
 */

  public class DepthPageTransformer implements PageTransformer {
private static float MIN_SCALE = 0.75f;

@SuppressLint("NewApi")
@Override
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);

    }
  }

}

时间

 private void initTime() {
    textView = (TextView) findViewById(R.id.tv);
    textView.setTextSize(30);
    textView.setTextColor(Color.parseColor("#ffffff"));
//        textView.setGravity(Gravity.BOTTOM);

    handler = new Handler() {
        public void handleMessage(Message msg) {
            textView.setText((String)msg.obj);
        }
    };

    new Thread(){
        @Override
        public void run() {
            try {
                while(true){
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
                    String str=sdf.format(new Date());
                    handler.sendMessage(handler.obtainMessage(100,str));
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }.start();
}

动态生成小圆点

  //dot_normal.xml
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="oval">
<corners android:radius="5dp" />
<size android:width="10dp" android:height="10dp" ></size>
<solid android:color="#6D788F" />
  </shape>

  //dot_selector.xml
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/dot_normal" android:state_selected="false"/>
<item android:drawable="@drawable/dot_focused" android:state_selected="true"/>
  </selector>

  private ImageView[] dotViews;//存放小圆点

  //初始化小圆点
  private void initDots() {
    numLayout = (LinearLayout) appLayout.findViewById(R.id.ll_pager_num);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(10,0,10,0);
    dotViews = new ImageView[pageCount];
    for (int i = 0; i < pageCount; i++){
        ImageView imageView = new ImageView(getActivity());
        imageView.setLayoutParams(layoutParams);
        imageView.setImageResource(R.drawable.dot_selector);
        if (i==0){
            imageView.setSelected(true);
        }else {
            imageView.setSelected(false);
        }
        dotViews[i] = imageView;//得到每个小圆点的引用,用于滑动页面
        numLayout.addView(imageView);//添加到布局里面显示
    }
}
  //动态切换小圆点
  viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            for(int i = 0; i < dotViews.length; i++){
                if(position == i){
                    dotViews[i].setSelected(true);
                }else {
                    dotViews[i].setSelected(false);
                }
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

获取安装的应用

  private List<ResolveInfo> mApps;//应用的数量

  private void loadApps() {
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  // mainIntent.addCategory(Intent.CATEGORY_CAR_MODE);
 mApps=getActivity().getPackageManager().queryIntentActivities(mainIntent, 0);
}

  //根据应用数动态生成页面
  inflater = LayoutInflater.from(getContext());
    //总的页数=总数/每页数量,并取整
    pageCount = (int) Math.ceil(mApps.size() * 1.0 / pageSize);
    mPagerList = new ArrayList<View>();
    for (int i = 0; i < pageCount; i++) {

        //每个页面都是inflate出一个新实例
        GridView gridView =  (GridView) inflater.inflate(R.layout.gridview, viewPager, false);
        gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));//取消GridView中Item选中时默认的背景色
        gridView.setAdapter(new GridViewAdapter(getContext(), mApps, i, pageSize));
        mPagerList.add(gridView);

        //跳转相应的apk
        final int finalI = i;
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int pos = position + finalI * pageSize;
                Log.e("pos ", "==="+pos);
                try {
                    ResolveInfo info = mApps.get(pos);
                    //该应用的包名
                    String pkg = info.activityInfo.packageName;
                    //应用的主activity类
                    String cls = info.activityInfo.name;
                    ComponentName componet = new ComponentName(pkg, cls);
                    Intent i = new Intent();
                    i.setComponent(componet);
                    startActivity(i);

                } catch (Exception e) {
                    e.printStackTrace();

                }

// Toast.makeText(getContext(), mDatas.get(pos).getName(), Toast.LENGTH_SHORT).show();
}
});

GridView适配器

public class GridViewAdapter extends BaseAdapter {

Context context;
private List<ResolveInfo> mApps;
private LayoutInflater inflater;
/**
 * 页数下标,从0开始(当前是第几页)
 */
private int curIndex;
/**
 * 每一页显示的个数
 */
private int pageSize;

public GridViewAdapter(Context context, List<ResolveInfo> mApps, int curIndex, int pageSize) {
    inflater = LayoutInflater.from(context);
    this.mApps = mApps;
    this.curIndex = curIndex;
    this.pageSize = pageSize;
    this.context=context;
}

@Override
public int getCount() {
    return mApps.size() > (curIndex + 1) * pageSize ? pageSize : (mApps.size() - curIndex * pageSize);
}

@Override
public Object getItem(int position) {
    return mApps.get(position + curIndex * pageSize);
}

@Override
public long getItemId(int position) {
    return position + curIndex * pageSize;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.item_gridview, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.tv = (TextView) convertView.findViewById(R.id.textView);
        viewHolder.iv = (ImageView) convertView.findViewById(R.id.imageView);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    /**
     * 在给View绑定显示的数据时,计算正确的position = position + curIndex * pageSize
     */
    int pos = position + curIndex * pageSize;
    ResolveInfo info = mApps.get(pos);
    viewHolder.tv.setText(info.activityInfo.loadLabel(context.getPackageManager()));
  // viewHolder.tv.setText("app");
    Log.e( "getView: ","======="+info.activityInfo.loadLabel(context.getPackageManager()) );
  //viewHolder.iv.setImageResource(R.mipmap.ic_launcher);
    viewHolder.iv.setImageDrawable(info.activityInfo.loadIcon(context.getPackageManager()));
    return convertView;
}


class ViewHolder {
    public TextView tv;
    public ImageView iv;
}
}

ViewPager适配器

  public class ViewPagerAdapter extends PagerAdapter {
  private List<View> mViewList;

  public ViewPagerAdapter(List<View> mViewList) {
    this.mViewList = mViewList;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView(mViewList.get(position));
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    container.addView(mViewList.get(position));
    return (mViewList.get(position));
}

@Override
public int getCount() {
    if (mViewList == null)
        return 0;
    return mViewList.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,125评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,293评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,054评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,077评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,096评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,062评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,988评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,817评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,266评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,486评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,646评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,375评论 5 342
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,974评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,621评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,642评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,538评论 2 352

推荐阅读更多精彩内容