android客户端首次进入轮播引导图(三)

2017-01-13 19:12:23来源:CSDN作者:qq_23195583人点击

获取服务器数据后的引导图代码,现在看一些用到的工具性代码:
记录是否是首次打开,以及获取图片接口的缓存操作
这个存放的是常量

package com.fanyafeng.recreation.cachemanager;import com.fanyafeng.recreation.util.StringUtil;/** * Author: fanyafeng * Data: 17/1/13 上午10:58 * Email: fanyafeng@live.cn */public class Start {    public final static String START_INFO = "start_info";    public final static String START_FIRST = "start_first";    public final static String START_IMAGE_LIST = "image_list";}

操作工具类:

package com.fanyafeng.recreation.cachemanager;import android.content.Context;import android.content.SharedPreferences;import com.fanyafeng.recreation.util.StringUtil;/** * Author: fanyafeng * Data: 17/1/13 上午10:56 * Email: fanyafeng@live.cn */public class StartManager {    public static void setStartCache(Context context, String keyName, String value) {        if (!StringUtil.isNullOrEmpty(value)) {            SharedPreferences sharedPreferences = context.getSharedPreferences(Start.START_INFO, Context.MODE_PRIVATE);            SharedPreferences.Editor editor = sharedPreferences.edit();            editor.putString(keyName, value);            editor.commit();        }    }    public static String getStartCache(Context context, String keyName) {        SharedPreferences sharedPreferences = context.getSharedPreferences(Start.START_INFO, Context.MODE_PRIVATE);        return sharedPreferences.getString(keyName, "");    }    public static void setFirstState(Context context, String keyName, boolean value) {        SharedPreferences sharedPreferences = context.getSharedPreferences(Start.START_INFO, Context.MODE_PRIVATE);        SharedPreferences.Editor editor = sharedPreferences.edit();        editor.putBoolean(keyName, !value);        editor.commit();    }    public static boolean getFirstState(Context context, String keyName) {        SharedPreferences sharedPreferences = context.getSharedPreferences(Start.START_INFO, Context.MODE_PRIVATE);        return !sharedPreferences.getBoolean(keyName, false);    }}

控制图片显示:

package com.fanyafeng.recreation.util;import android.net.Uri;import android.support.annotation.NonNull;import com.facebook.common.util.UriUtil;import com.facebook.drawee.backends.pipeline.Fresco;import com.facebook.drawee.interfaces.DraweeController;import com.facebook.drawee.view.SimpleDraweeView;import com.facebook.imagepipeline.request.ImageRequest;/** * Author: fanyafeng * Data: 16/7/9 12:04 * Email: fanyafeng@live.cn */public class FrescoUtil {    /**     * 加载app内非动图     *     * @param simpleDraweeView view控件     * @param resId            资源比例     * @param aspectRatio      图片长宽比例     */    public static void loadPicInApp(@NonNull SimpleDraweeView simpleDraweeView, @NonNull int resId, float aspectRatio) {        if (simpleDraweeView == null)            return;        Uri uri = new Uri.Builder()                .scheme(UriUtil.LOCAL_RESOURCE_SCHEME)                .path(String.valueOf(resId))                .build();        if (aspectRatio > 0) {            simpleDraweeView.setAspectRatio(aspectRatio);        }        simpleDraweeView.setImageURI(uri);    }    /**     * 加载app内非动图     *     * @param simpleDraweeView     * @param resId     */    public static void loadPicInApp(@NonNull SimpleDraweeView simpleDraweeView, @NonNull int resId) {        if (simpleDraweeView == null)            return;        Uri uri = new Uri.Builder()                .scheme(UriUtil.LOCAL_RESOURCE_SCHEME)                .path(String.valueOf(resId))                .build();        simpleDraweeView.setImageURI(uri);    }    /**     * @param simpleDraweeView     * @param resId     * @param aspectRatio     */    public static void loadGifPicInApp(@NonNull SimpleDraweeView simpleDraweeView, @NonNull int resId, float aspectRatio) {        if (simpleDraweeView == null) {            return;        }        Uri uri = new Uri.Builder()                .scheme(UriUtil.LOCAL_RESOURCE_SCHEME)                .path(String.valueOf(resId))                .build();        DraweeController draweeController = Fresco.newDraweeControllerBuilder()                .setUri(uri)                .setAutoPlayAnimations(true)                .build();        if (aspectRatio > 0) {            simpleDraweeView.setAspectRatio(aspectRatio);        }        simpleDraweeView.setController(draweeController);    }    /**     * @param simpleDraweeView     * @param resId     */    public static void loadGifPicInApp(@NonNull SimpleDraweeView simpleDraweeView, @NonNull int resId) {        if (simpleDraweeView == null) {            return;        }        Uri uri = new Uri.Builder()                .scheme(UriUtil.LOCAL_RESOURCE_SCHEME)                .path(String.valueOf(resId))                .build();        DraweeController draweeController = Fresco.newDraweeControllerBuilder()                .setUri(uri)                .setAutoPlayAnimations(true)                .build();        simpleDraweeView.setController(draweeController);    }    /**     * @param simpleDraweeView     * @param gifUrl     * @param aspectRatio     */    public static void loadGifPicOnNet(SimpleDraweeView simpleDraweeView, @NonNull String gifUrl, float aspectRatio) {        DraweeController draweeController = Fresco.newDraweeControllerBuilder()                .setUri(Uri.parse(gifUrl))                .setAutoPlayAnimations(true)                .build();        if (aspectRatio > 0) {            simpleDraweeView.setAspectRatio(aspectRatio);        }        simpleDraweeView.setController(draweeController);    }    /**     * @param simpleDraweeView     * @param gifUrl     */    public static void loadGifPicOnNet(SimpleDraweeView simpleDraweeView, @NonNull String gifUrl) {        DraweeController draweeController = Fresco.newDraweeControllerBuilder()                .setUri(Uri.parse(gifUrl))                .setAutoPlayAnimations(true)                .build();        simpleDraweeView.setController(draweeController);    }    public static void loadPicOnNet(SimpleDraweeView simpleDraweeView, @NonNull String imgUrl) {        simpleDraweeView.setImageURI(imgUrl);    }    public static void loadPicOnNet(SimpleDraweeView simpleDraweeView, @NonNull Uri imgUri) {        simpleDraweeView.setImageURI(imgUri);    }    public static void loadPicOnNet(SimpleDraweeView simpleDraweeView, @NonNull String imgUri, float aspectRatio) {        simpleDraweeView.setAspectRatio(aspectRatio);        simpleDraweeView.setImageURI(imgUri);    }    /**     * 多图加载     *     * @param simpleDraweeView     * @param highUrl          高分辨率     * @param lowUrl           低分辨率     */    public static void loadPicOnNet(SimpleDraweeView simpleDraweeView, Uri lowUri, Uri highUri) {        DraweeController draweeController = Fresco.newDraweeControllerBuilder()                .setLowResImageRequest(ImageRequest.fromUri(lowUri))                .setImageRequest(ImageRequest.fromUri(highUri))                .setOldController(simpleDraweeView.getController())                .build();        simpleDraweeView.setController(draweeController);    }    public static void loadPicOnNet(SimpleDraweeView simpleDraweeView, String lowUrl, String highUrl) {        DraweeController draweeController = Fresco.newDraweeControllerBuilder()                .setLowResImageRequest(ImageRequest.fromUri(lowUrl))                .setImageRequest(ImageRequest.fromUri(highUrl))                .setOldController(simpleDraweeView.getController())                .build();        simpleDraweeView.setController(draweeController);    }    public static void loadFilePic(SimpleDraweeView simpleDraweeView, String imgUrl) {        simpleDraweeView.setImageURI("file://" + imgUrl);    }    public static void loadFilePic(SimpleDraweeView simpleDraweeView, String imgUrl, float aspectRatio) {        if (aspectRatio > 0) {            simpleDraweeView.setAspectRatio(aspectRatio);        }        simpleDraweeView.setImageURI("file://" + imgUrl);    }    public static void loadContentPic(SimpleDraweeView simpleDraweeView, String imgUrl) {        simpleDraweeView.setImageURI("content://" + imgUrl);    }    public static void loadContentPic(SimpleDraweeView simpleDraweeView, String imgUrl, float aspectRatio) {        if (aspectRatio > 0) {            simpleDraweeView.setAspectRatio(aspectRatio);        }        simpleDraweeView.setImageURI("content://" + imgUrl);    }    public static void loadResPic(SimpleDraweeView simpleDraweeView, String imgUrl) {        simpleDraweeView.setImageURI("res://" + imgUrl);    }    public static void loadResPic(SimpleDraweeView simpleDraweeView, String imgUrl, float aspectRatio) {        if (aspectRatio > 0) {            simpleDraweeView.setAspectRatio(aspectRatio);        }        simpleDraweeView.setImageURI("res://" + imgUrl);    }}

图片的复制等操作:

package com.fanyafeng.recreation.util;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.Typeface;import android.net.Uri;import com.commit451.nativestackblur.NativeStackBlur;import com.facebook.binaryresource.FileBinaryResource;import com.facebook.cache.common.SimpleCacheKey;import com.facebook.common.executors.UiThreadImmediateExecutorService;import com.facebook.common.references.CloseableReference;import com.facebook.datasource.DataSource;import com.facebook.drawee.backends.pipeline.Fresco;import com.facebook.drawee.backends.pipeline.PipelineDraweeController;import com.facebook.drawee.controller.BaseControllerListener;import com.facebook.drawee.interfaces.DraweeController;import com.facebook.drawee.view.SimpleDraweeView;import com.facebook.imagepipeline.bitmaps.PlatformBitmapFactory;import com.facebook.imagepipeline.common.ImageDecodeOptions;import com.facebook.imagepipeline.common.ResizeOptions;import com.facebook.imagepipeline.core.ImagePipeline;import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber;import com.facebook.imagepipeline.image.CloseableImage;import com.facebook.imagepipeline.image.ImageInfo;import com.facebook.imagepipeline.request.BasePostprocessor;import com.facebook.imagepipeline.request.ImageRequest;import com.facebook.imagepipeline.request.ImageRequestBuilder;import com.facebook.imagepipeline.request.Postprocessor;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;/** * Author: fanyafeng * Data: 16/7/28 10:57 * Email: fanyafeng@live.cn */public class FrescoDealPicUtil {    /**     * 裁剪图片     *     * @param simpleDraweeView     * @param picUrl     * @param showWidth,需要显示的图片的宽度     * @param showHeight,需要显示图片的高度     * @param cutWidth,裁剪图片的宽的起点     * @param cutHeight,裁剪图片高的起点     */    public static void getCutedPic(SimpleDraweeView simpleDraweeView, String picUrl, final int showWidth, final int showHeight, final int cutWidth, final int cutHeight) {        Postprocessor postprocessor = new BasePostprocessor() {            @Override            public CloseableReference<Bitmap> process(Bitmap sourceBitmap, PlatformBitmapFactory bitmapFactory) {                CloseableReference<Bitmap> bitmapRef = bitmapFactory.createBitmap(                        showWidth,                        showHeight);                int aimWidth = sourceBitmap.getWidth() - cutWidth;                int aimHeight = sourceBitmap.getHeight() - cutHeight;                try {                    Bitmap destBitmap = bitmapRef.get();                    for (int x = 0; x < showWidth; x++) {                        for (int y = 0; y < showHeight; y++) {                            destBitmap.setPixel(x, y, sourceBitmap.getPixel(aimWidth + x, aimHeight + y));                        }                    }                    return CloseableReference.cloneOrNull(bitmapRef);                } finally {                    CloseableReference.closeSafely(bitmapRef);                }            }        };        ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(picUrl))                .setPostprocessor(postprocessor)                .build();        PipelineDraweeController pipelineDraweeController = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()                .setImageRequest(imageRequest)                .setOldController(simpleDraweeView.getController())                .setTapToRetryEnabled(true)//点击重试                .build();        simpleDraweeView.setController(pipelineDraweeController);    }    /**     * 图片加水印(字)     *     * @param simpleDraweeView     * @param imgUrl     * @param mark             需要加的水印,限制为字     * @param color            水印字体颜色     * @param alpha            水印透明度     */    public static void setTextWaterMark(SimpleDraweeView simpleDraweeView, String imgUrl, final String mark, final int color, final int alpha) {        Postprocessor postprocessor = new BasePostprocessor() {            @Override            public void process(Bitmap bitmap) {                super.process(bitmap);                int width = bitmap.getWidth();                int height = bitmap.getHeight();                Canvas canvas = new Canvas(bitmap);                Paint paint = new Paint();                paint.setStrokeWidth(10);                String familyName = "楷体";                Typeface font = Typeface.create(familyName, Typeface.BOLD);                paint.setColor(color);                paint.setTypeface(font);                paint.setTextSize(60);                paint.setAlpha(alpha);                Rect rect = new Rect();                paint.getTextBounds(mark, 0, mark.length(), rect);                int textWidth = rect.width();                canvas.drawText(mark, width - textWidth - 50, height - 50, paint);            }        };        ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(imgUrl))                .setPostprocessor(postprocessor)                .build();        PipelineDraweeController pipelineDraweeController = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()                .setImageRequest(imageRequest)                .setOldController(simpleDraweeView.getController())                .build();        simpleDraweeView.setController(pipelineDraweeController);    }    /**     * 图片加水印(图)     *     * @param context     * @param simpleDraweeView     * @param picUrl     * @param picId            水印图的id     */    public static void setPicWaterMark(final Context context, SimpleDraweeView simpleDraweeView, String picUrl, final int picId) {        Postprocessor postprocessor = new BasePostprocessor() {            @Override            public void process(Bitmap bitmap) {                super.process(bitmap);                int width = bitmap.getWidth();                int height = bitmap.getHeight();                Canvas canvas = new Canvas(bitmap);                Paint paint = new Paint();                paint.setAlpha(255);                Bitmap bitma = BitmapFactory.decodeResource(context.getResources(), picId);                int markWidth = bitma.getWidth();                canvas.drawBitmap(bitma, width - markWidth - 50, 50, paint);            }        };        ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(picUrl))                .setPostprocessor(postprocessor)                .build();        PipelineDraweeController pipelineDraweeController = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()                .setImageRequest(imageRequest)                .setOldController(simpleDraweeView.getController())                .build();        simpleDraweeView.setController(pipelineDraweeController);    }    /**     * resize 图片     *     * @param uri     * @param draweeView     */    public static void showThumb(Uri uri, SimpleDraweeView draweeView) {        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)//                .setResizeOptions(new ResizeOptions(.dip2px(144), DensityUtil.dip2px(144)))                .build();        DraweeController controller = Fresco.newDraweeControllerBuilder()                .setImageRequest(request)                .setOldController(draweeView.getController())                .setControllerListener(new BaseControllerListener<ImageInfo>())                .build();        draweeView.setController(controller);    }    /**     * 异步     *     * @param context     * @param picUrl     * @return     */    public static Bitmap getBitmap(Context context, String picUrl) {        final Bitmap[] bitMap = {null};        Uri uri = Uri.parse(picUrl);        ImageDecodeOptions decodeOptions = ImageDecodeOptions.newBuilder()                .build();        ImageRequest imageRequest = ImageRequestBuilder                .newBuilderWithSource(uri)                .setImageDecodeOptions(decodeOptions)                .setAutoRotateEnabled(true)                .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.FULL_FETCH)                .setProgressiveRenderingEnabled(false)//                .setResizeOptions()                .build();        ImagePipeline imagePipeline = Fresco.getImagePipeline();        DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, context);        dataSource.subscribe(new BaseBitmapDataSubscriber() {            @Override            protected void onNewResultImpl(Bitmap bitmap) {                bitMap[0] = bitmap;            }            @Override            protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {            }        }, UiThreadImmediateExecutorService.getInstance());        return bitMap[0];    }    public static void download(Context context, String url) {        ImageRequest request = ImageRequestBuilder.                newBuilderWithSource(Uri.parse(url))                .setAutoRotateEnabled(true)                .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.FULL_FETCH)                .setProgressiveRenderingEnabled(false)                .build();        ImagePipeline imagePipeline = Fresco.getImagePipeline();        imagePipeline.prefetchToDiskCache(request, context);    }    public static boolean isExist(String imgUrl) {        FileBinaryResource fileBinaryResource = (FileBinaryResource) Fresco.getImagePipelineFactory()                .getMainFileCache().getResource(new SimpleCacheKey(imgUrl));        if (fileBinaryResource == null) {            return false;        } else {            return true;        }    }    /**     * 图片拷贝     *     * @param imgUrl     * @param newPath     * @param fileName     * @return     */    public static boolean copyPicFile(String imgUrl, String newPath, String fileName) {        FileBinaryResource fileBinaryResource = (FileBinaryResource) Fresco.getImagePipelineFactory()                .getMainFileCache().getResource(new SimpleCacheKey(imgUrl));        if (fileBinaryResource == null) {            return false;        }        File oldfile = fileBinaryResource.getFile();        boolean isok = true;        try {            int bytesum = 0;            int byteread = 0;            if (oldfile.exists()) { //文件存在时                InputStream inStream = new FileInputStream(oldfile); //读入原文件                if (!new File(newPath).exists()) {                    new File(newPath).mkdirs();                }                String myPath = newPath + File.separator + fileName;                FileOutputStream fs = new FileOutputStream(myPath);                byte[] buffer = new byte[1024];                int length;                while ((byteread = inStream.read(buffer)) != -1) {                    bytesum += byteread; //字节数 文件大小                    fs.write(buffer, 0, byteread);                }                fs.flush();                fs.close();                inStream.close();            } else {                isok = false;            }        } catch (Exception e) {            isok = false;        }        return isok;    }    public static void loadUrl(String url, SimpleDraweeView draweeView, BasePostprocessor processor, int width, int height,                               BaseControllerListener listener) {        load(Uri.parse(url), draweeView, processor, width, height, listener);    }    public static void load(Uri uri, SimpleDraweeView draweeView, BasePostprocessor processor, int width, int height,                            BaseControllerListener listener) {        ResizeOptions resizeOptions = null;        if (width > 0 && height > 0) {            resizeOptions = new ResizeOptions(width, height);        }        ImageRequest request =                ImageRequestBuilder.newBuilderWithSource(uri)                        .setPostprocessor(processor)//                        .setResizeOptions(resizeOptions)                        //缩放,在解码前修改内存中的图片大小, 配合Downsampling可以处理所有图片,否则只能处理jpg,                        // 开启Downsampling:在初始化时设置.setDownsampleEnabled(true)                        .setProgressiveRenderingEnabled(true)//支持图片渐进式加载                        .setAutoRotateEnabled(true) //如果图片是侧着,可以自动旋转                        .build();        PipelineDraweeController controller =                (PipelineDraweeController) Fresco.newDraweeControllerBuilder()                        .setImageRequest(request)                        .setControllerListener(listener)                        .setOldController(draweeView.getController())                        .setAutoPlayAnimations(true) //自动播放gif动画                        .build();        draweeView.setController(controller);    }    private Bitmap fastBlur(Bitmap bkg, int radius, int downSampling) {        if (downSampling < 2) {            downSampling = 2;        }        Bitmap smallBitmap = Bitmap.createScaledBitmap(bkg, bkg.getWidth() / downSampling, bkg.getHeight() / downSampling, true);        return NativeStackBlur.process(smallBitmap, radius);    }}

展示轮播图的fragement,稍微做了一下改动:

package com.fanyafeng.recreation.fragment;import android.content.Context;import android.net.Uri;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import com.facebook.drawee.view.SimpleDraweeView;import com.fanyafeng.recreation.R;import com.fanyafeng.recreation.util.FrescoUtil;import com.fanyafeng.recreation.util.StringUtil;public class StartPagerFragment extends Fragment {    private static final String ARG_PARAM1 = "param1";    private static final String ARG_PARAM2 = "param2";    private static final String ARG_PARAM3 = "param3";    private String mParam1;    private int mParam2=0;    private String mParam3;    private SimpleDraweeView sdvStart;    public StartPagerFragment() {        // Required empty public constructor    }    public static StartPagerFragment newInstance(String param1, int param2, String param3) {        StartPagerFragment fragment = new StartPagerFragment();        Bundle args = new Bundle();        args.putString(ARG_PARAM1, param1);        args.putInt(ARG_PARAM2, param2);        args.putString(ARG_PARAM3, param3);        fragment.setArguments(args);        return fragment;    }    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        if (getArguments() != null) {            mParam1 = getArguments().getString(ARG_PARAM1);            mParam2 = getArguments().getInt(ARG_PARAM2);            mParam3 = getArguments().getString(ARG_PARAM3);        }    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        // Inflate the layout for this fragment        View view = inflater.inflate(R.layout.fragment_start_pager, container, false);        sdvStart = (SimpleDraweeView) view.findViewById(R.id.sdvStart);        return view;    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        if (mParam2 != 0) {            FrescoUtil.loadGifPicInApp(sdvStart, mParam2);        }        if (!StringUtil.isNullOrEmpty(mParam3)) {            FrescoUtil.loadGifPicOnNet(sdvStart, mParam3);        }    }}

操作fragment的activity:这个基本上也是可以配置的,和文章一种差不多,没有大改,当初写的时候基本上就考虑到这个了

package com.fanyafeng.recreation.activity;import android.content.Intent;import android.graphics.Matrix;import android.os.Bundle;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentStatePagerAdapter;import android.support.v4.view.ViewCompat;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.util.DisplayMetrics;import android.util.Log;import android.view.View;import android.view.ViewTreeObserver;import android.widget.Button;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RelativeLayout;import com.fanyafeng.recreation.R;import com.fanyafeng.recreation.BaseActivity;import com.fanyafeng.recreation.bean.StartBean;import com.fanyafeng.recreation.fragment.StartPagerFragment;import com.fanyafeng.recreation.util.MyUtils;import java.util.ArrayList;import java.util.List;//需要搭配Baseactivity,这里默认为Baseactivity,并且默认BaseActivity为包名的根目录public class FirstStartActivity extends BaseActivity {    private ViewPager startViewPager;    private StartPagerAdapter startPagerAdapter;    private List<Fragment> fragmentList = new ArrayList<>();    private Button btnFirstStart;    private int maxX = 0;    private ImageView ivMovementCircle;    private static int[] startPage = new            int[]{R.drawable.start_one, R.drawable.start_two, R.drawable.start_three, R.drawable.start_four, R.drawable.start_five};    private int totalSize = startPage.length;    private RelativeLayout layoutStartRoot;    private float moveCircleY = 0;    private ArrayList<StartBean> startBeanList = new ArrayList<>();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_first_start);        //这里默认使用的是toolbar的左上角标题,如果需要使用的标题为中心的采用下方注释的代码,将此注释掉即可        title = getString(R.string.title_activity_first_start);        isShowToolbar = false;        if (getIntent().getParcelableArrayListExtra("startBeanList") != null) {            startBeanList = getIntent().getParcelableArrayListExtra("startBeanList");            totalSize = startBeanList.size();        }        initView();        initData();        initCursorPosition();    }    //初始化UI控件    private void initView() {        layoutStartRoot = (RelativeLayout) findViewById(R.id.layoutStartRoot);        ivMovementCircle = (ImageView) findViewById(R.id.ivMovementCircle);        btnFirstStart = (Button) findViewById(R.id.btnFirstStart);        startViewPager = (ViewPager) findViewById(R.id.startViewPager);        for (int i = 0; i < totalSize; i++) {            fragmentList.add(new StartPagerFragment());        }    }    private void initCursorPosition() {        DisplayMetrics displayMetrics = new DisplayMetrics();        this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);        final int width = displayMetrics.widthPixels;        maxX = width;        Matrix matrix = new Matrix();        matrix.postTranslate(width / (totalSize + 1) - MyUtils.dip2px(this, 4), 0);        ivMovementCircle.setImageMatrix(matrix);        ivMovementCircle.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {            @Override            public void onGlobalLayout() {                if (ivMovementCircle.getY() != 0) {                    moveCircleY = ivMovementCircle.getY();                    for (int i = 0; i < totalSize; i++) {                        ImageView imageView = new ImageView(FirstStartActivity.this);                        imageView.setImageDrawable(getResources().getDrawable(R.drawable.white_shape_circle));                        imageView.setX(width / (totalSize + 1) * (i + 1) - MyUtils.dip2px(FirstStartActivity.this, 3));                        imageView.setY(moveCircleY);                        layoutStartRoot.addView(imageView);                        ivMovementCircle.bringToFront();                    }                } else {                    return;                }                ivMovementCircle.getViewTreeObserver().removeGlobalOnLayoutListener(this);            }        });    }    //初始化数据    private void initData() {        startPagerAdapter = new StartPagerAdapter(getSupportFragmentManager(), fragmentList);        startViewPager.setAdapter(startPagerAdapter);        startViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {            @Override            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {                if (maxX != 0 && positionOffsetPixels != 0) {                    float alph = 255 * positionOffsetPixels / maxX;                    if (position == totalSize - 2) {                        btnFirstStart.setVisibility(View.VISIBLE);                        btnFirstStart.getBackground().setAlpha((int) alph);                        btnFirstStart.setAlpha(alph);                    } else {                        btnFirstStart.setVisibility(View.GONE);                    }                }                if (positionOffsetPixels != 0) {                    ViewCompat.setTranslationX(ivMovementCircle, maxX / (totalSize + 1) * position + positionOffsetPixels / (totalSize + 1));                }            }            @Override            public void onPageSelected(int position) {            }            @Override            public void onPageScrollStateChanged(int state) {            }        });    }    class StartPagerAdapter extends FragmentStatePagerAdapter {        private List<Fragment> fragmentList;        public StartPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {            super(fm);            this.fragmentList = fragmentList;        }        @Override        public Fragment getItem(int position) {            Bundle bundle = new Bundle();            bundle.putString("param1", String.valueOf(position));            if (startBeanList.size() > 0) {                bundle.putString("param3", startBeanList.get(position).getImgUrl());            } else {                bundle.putInt("param2", startPage[position]);            }            Fragment fragment = fragmentList.get(position);            fragment.setArguments(bundle);            return fragment;        }        @Override        public int getCount() {            return fragmentList.size();        }    }    @Override    public void onClick(View v) {        super.onClick(v);        switch (v.getId()) {            case R.id.btnFirstStart:                finish();                break;            case R.id.btnFirstFinish:                finish();                break;        }        startActivity(new Intent(this, MainActivity.class));    }}

这基本就是用到工具性的东西,不用大改的,基本上是可以配置的。

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台