那些用过的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;
}
}
    原文作者:Trap_余生
    原文地址: https://www.jianshu.com/p/0b687b09026f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞