Glide自定义ModelLoader来加载多张候选图片

Glide自定义ModelLoader来加载多张候选图片

需求

使用过Glide这个框架的同学大概知道,Glide从网络上加载图片时,只能加载一张图片,失败之后,可以选择显示占位图,如下面这段代码:

 Glide.with(context).load(url)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.DATA)
                .apply(new RequestOptions().dontAnimate())
                .placeholder(placeholderRes)
                .into(imageView);

但不知道大家有没有这样的需求:

如果我们有多张候选的图片url,需要在加载出错之后,尝试从下一个url中去进行加载,直到完成所有url的尝试。

所以我们希望在使用Glide加载图片时,大概的使用方式能如下面代码所示:

ImgUrlWrapper.java

/*
* ImgUrlWrapper类,将所有的url打包到一起
*/
public class ImgUrlWrapper {
	private List<String> urls;
	public ImgUrlWrapper(List<String> urls) {
		this.urls = urls;
	}
	public void setUrls(List<String> urls) {
		this.urls = urls;
	}

	public List<String> getUrls() {
		return this.urls;
	}
}
/*
* 一次图片加载失败之后,尝试加载其他的候选图片 
*/
 public static void displayWithCustomModule(Context context, ImageWrapper imageWrapper, int placeholderRes,
            ImageView imageView) {
        GlideApp.with(context)
                .load(imageWrapper)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.DATA).apply(new RequestOptions().dontAnimate())
                .placeholder(placeholderRes)
                .into(imageView);
    }

解题思路

读过Glide(version 4.11.0)源码的同学,大概能知道,Glide是通过ModelLoader<Model, Data>完成图片的加载过程的封装的,由DataFetcher<T>负责完成具体的数据加载,AppGlideModule负责注册一组组件,以在使用Glide的注释处理器时在应用程序中初始化Glide时使用。

/*
 * @param <Model> 资源的解码类型(转换前),比如:File, url/InputStream
 * @param <Data> 可用于ResourceDecoder解码资源的数据类型(转换后),比如:Bitmap, Drawable等等
 */
public interface ModelLoader<Model, Data> {

  /**
   * Contains a set of {@link com.bumptech.glide.load.Key Keys} identifying the source of the load,
   * alternate cache keys pointing to equivalent data, and a {@link
   * com.bumptech.glide.load.data.DataFetcher} that can be used to fetch data not found in cache.
   *
   * @param <Data> The type of data that well be loaded.
   */
  class LoadData<Data> {
    public final Key sourceKey;
    public final List<Key> alternateKeys;
    public final DataFetcher<Data> fetcher;

    public LoadData(@NonNull Key sourceKey, @NonNull DataFetcher<Data> fetcher) {
      this(sourceKey, Collections.<Key>emptyList(), fetcher);
    }

    public LoadData(
        @NonNull Key sourceKey,
        @NonNull List<Key> alternateKeys,
        @NonNull DataFetcher<Data> fetcher) {
      this.sourceKey = Preconditions.checkNotNull(sourceKey);
      this.alternateKeys = Preconditions.checkNotNull(alternateKeys);
      this.fetcher = Preconditions.checkNotNull(fetcher);
    }
  }

  /**
   * 返回一个包裹了DataFetcher的LoadData, 即创建加载数据
   */
  @Nullable
  LoadData<Data> buildLoadData(
      @NonNull Model model, int width, int height, @NonNull Options options);

  /**
   * 返回次Loader是否能够处理对应Model的数据
   * @return true if this loader can handle the model and false otherwise
   */
  boolean handles(@NonNull Model model);
}

上面源码中通过buildLoadData方法创建LoadData,而LoadData中的DataFetcher如下:

/**
 * 负载加载数据的接口
 * @param <T> The type of data to be loaded (InputStream, byte[], File etc).
 */
public interface DataFetcher<T> {
  interface DataCallback<T> {

    /**
     * Called with the loaded data if the load succeeded, or with {@code null} if the load failed.
     */
    void onDataReady(@Nullable T data);

    /**
     * Called when the load fails.
     */
    void onLoadFailed(@NonNull Exception e);
  }

  /**
   * 从一个可以被解码的资源拉取数据
   */
  void loadData(@NonNull Priority priority, @NonNull DataCallback<? super T> callback);

  /**
   * 清理或回收资源
   */
  void cleanup();

  /**
  * 退出加载
   */
  void cancel();

  /** Returns the class of the data this fetcher will attempt to obtain. */
  @NonNull
  Class<T> getDataClass();

  /** Returns the {@link com.bumptech.glide.load.DataSource} this fetcher will return data from. */
  @NonNull
  DataSource getDataSource();
}

public abstract class AppGlideModule extends LibraryGlideModule implements AppliesOptions {

  public boolean isManifestParsingEnabled() {
    return true;
  }

  @Override
  public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
    // Default empty impl.
  }
}

public abstract class LibraryGlideModule implements RegistersComponents {
  @Override
  public void registerComponents(
      @NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
    // Default empty impl.
  }
}

实现过程

先定义一个Model(即ModelLoader<Model, Data>中的Model),我们把所有候选url都打包进去

public class ImgUrlWrapper {

    private List<String> mUrls;

    public ImgUrlWrapper(List<String> urls) {
        mUrls = urls;
    }

    public List<String> getUrls() {
        return mUrls;
    }

    public void setUrls(List<String> urls) {
        mUrls = urls;
    }
}

定义我们自己的DataFetcher,并参考Glide官方的HttpUrlFetcher来实现我们的需求(候选图片的加载)。我们在上面提到过,DataFetcher负责实现具体的数据加载过程。

下面代码中省略部分都是从HttpUrlFetcher拷贝过来的,我们主要关注loadData方法

public class UrlsFetcher implements DataFetcher<InputStream> {

    private static final String TAG = "HttpUrlFetcher";
    private static final int MAXIMUM_REDIRECTS = 5;
    private static final int INVALID_STATUS_CODE = -1;
    private final int timeout = 2500;
    private HttpUrlConnectionFactory connectionFactory;
    static HttpUrlConnectionFactory DEFAULT_CONNECTION_FACTORY =
            new DefaultHttpUrlConnectionFactory();
    private HttpURLConnection urlConnection;
    private InputStream stream;
    private volatile boolean isCancelled;
    private final List<String> mUrls;
   
    private int urlCount = 0;

    public UrlsFetcher(List<String> urls) {
        this.mUrls = urls;
        connectionFactory = DEFAULT_CONNECTION_FACTORY;
        urlCount = urls.size();
    }

    @Override
    public void loadData(@NonNull Priority priority,
            @NonNull DataCallback<? super InputStream> callback) {
        // 从最后一个图片url开始加载图片
        urlCount--;
        try {
            GlideUrl glideUrl = new GlideUrl(mUrls.get(urlCount));
            InputStream result = loadDataWithRedirects(glideUrl.toURL(), 0, null,
                    glideUrl.getHeaders());
            // 图片加载完成,直接回调
            callback.onDataReady(result);
        } catch (IOException e) {
        	// 如果加载失败,尝试加载下一张图
            if (urlCount >= 0) {
                // 打印日志来验证是否尝试从其他地址加载图片
                Log.d(TAG, "retry next url " + mUrls.get(urlCount));
                loadData(priority, callback);
            } else {
                if (Log.isLoggable(TAG, Log.DEBUG)) {
                    Log.d(TAG, "Failed to load data for url", e);
                }
                // 所有图片加载失败的回调
                callback.onLoadFailed(e);
            }
        } 
    }
   ...
}

完成自定义的DataFetcher,我们定义一个ModelLoader来封装这个加载过程:

public class UrlsLoader implements ModelLoader<ImgUrlWrapper, InputStream> {
    @Nullable
    @Override
    public LoadData<InputStream> buildLoadData(@NonNull ImgUrlWrapper wrapper, int width, int height,
            @NonNull Options options) {
        // 返回LoadData
        return new LoadData<>(new ObjectKey(wrapper), new UrlsFetcher(wrapper.getUrls()));
    }

    @Override
    public boolean handles(@NonNull ImgUrlWrapper wrapper) {
   		// 只允许带有http和https的链接地址
        for (String url : wrapper.getUrls()) {
            if (!url.startsWith("http") || !url.startsWith("https")) {
                return false;
            }
        }
        return true;
    }

    public static class Factory implements ModelLoaderFactory<ImgUrlWrapper, InputStream> {
        @NonNull
        @Override
        public ModelLoader<ImgUrlWrapper, InputStream> build(
                @NonNull MultiModelLoaderFactory multiFactory) {
            return new UrlsLoader();
        }
        @Override
        public void teardown() {

        }
    }
}

完成下面最后一步,我们就可以验证我们的思路是否正确了

/*
* 注解@GlideModule不能缺,不然无法编译生成我们想要的GlideApp
* AppGlideModule中有个注释,如下:
* Classes that extend {@link AppGlideModule} must be annotated with {@link
 * com.bumptech.glide.annotation.GlideModule} to be processed correctly.
*/
@GlideModule
public class UrlsModule extends AppGlideModule {
    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide,
            @NonNull Registry registry) {
        super.registerComponents(context, glide, registry);
        // 完成注册
        glide.getRegistry().append(ImgUrlWrapper.class, InputStream.class, new UrlsLoader.Factory());
    }

    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        super.applyOptions(context, builder);
    }
}

OK,我们使用写一个Demo来验证我们的想法是否正确:

public class MainActivity extends AppCompatActivity {

    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageView = findViewById(R.id.img);
         // 以百度搜索的Logo地址为例,下面所有地址中只有
        // https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png是正确的,
        // 其他地址都无法获取到图片
        List<String> urls = new ArrayList() {
            {
            add("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png");
            add("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d");
            add("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6");
            add("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6c");
            add("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf");
            add("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.p");
            add("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.pn");
            }
        };
        GlideApp.with(this.getApplicationContext())
                .load(new ImgUrlWrapper(urls))
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .apply(new RequestOptions().dontAnimate())
                .placeholder(R.mipmap.ic_launcher)
                .into(mImageView);
    }
}
2021-03-31 20:15:33.639 12972-12997/com.example.glidex D/HttpUrlFetcher: retry next url https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.pn
2021-03-31 20:15:33.843 12972-12997/com.example.glidex D/HttpUrlFetcher: retry next url https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.p
2021-03-31 20:15:34.051 12972-12997/com.example.glidex D/HttpUrlFetcher: retry next url https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf
2021-03-31 20:15:34.226 12972-12997/com.example.glidex D/HttpUrlFetcher: retry next url https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6c
2021-03-31 20:15:34.433 12972-12997/com.example.glidex D/HttpUrlFetcher: retry next url https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6
2021-03-31 20:15:34.642 12972-12997/com.example.glidex D/HttpUrlFetcher: retry next url https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d

从上面的日志中,我们可以验证我们的思路和实现方式都是OK的。如果各位同学还有其他的想法,也可以借鉴这种思路来实现。

Demo源码

Gitee

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
应用启动页自定义跳转计时器View Demo: CircleTextProgressbar.java: package com.demo.startpageskiptimerdemo.widget; import android.content.Context; import android.content.res.ColorStateList; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.text.TextUtils; import android.util.AttributeSet; import com.demo.startpageskiptimerdemo.R; /** * Created by Administrator on 2016/8/26. (自定义TextView,用于欢迎页跳过图标) */ public class CircleTextProgressbar extends android.support.v7.widget.AppCompatTextView { /** * 外部轮廓的颜色。 */ private int outLineColor = Color.BLACK; /** * 外部轮廓的宽度。 */ private int outLineWidth = 2; /** * 内部圆的颜色。 */ private ColorStateList inCircleColors = ColorStateList.valueOf(Color.TRANSPARENT); /** * 中心圆的颜色。 */ private int circleColor; /** * 进度条的颜色。 */ private int progressLineColor = Color.WHITE; /** * 进度条的宽度。 */ private int progressLineWidth = 8; /** * 画笔。 */ private Paint mPaint = new Paint(); /** * 进度条的矩形区域。 */ private RectF mArcRect = new RectF(); /** * 进度。 */ private int progress = 100; /** * 进度条类型。 */ private ProgressType mProgressType = ProgressType.COUNT; /** * 进度倒计时时间。 */ private long timeMillis = 5000; /** * View的显示区域。 */ final Rect bounds = new Rect(); /** * 进度条通知。 */ private OnCountdownProgressListener mCountdownProgressListener; /** * Listener what。 */ private int listenerWhat = 0; private String seconds; public CircleTextProgressbar(Context context) { this(context, null); } public CircleTextProgressbar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleTextProgressbar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initialize(context, attrs); } public void setSeconds(String seconds) { this.seconds = seconds; invalidate(); } /** * 初始化。 * * @param context 上下文。 * @param attributeSet 属性。 */ private void initialize(Context context, AttributeSet attributeSet) { mPaint.setAntiAlias(true); TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.CircleTextProgressbar); if (typedArray.hasValue(R.styleable.CircleTextProgressbar_in_circle_color)) inCircleColors = typedArray.getColorStateList(R.styleable.CircleTextProgressbar_in_circle_color); else inCircleColors = ColorStateList.valueOf(Color.TRANSPARENT); circleColor = inCircleColors.getColorForState(getDrawableState(), Color.TRANSPARENT); typedArray.recycle(); } /** * 设置外部轮廓的颜色。 * * @param outLineColor 颜色值。 */ // public void setOutLineColor(@ColorInt int outLineColor) { public void setOutLineColor(int outLineColor) { this.outLineColor = outLineColor; invalidate(); } /** * 设置外部轮廓的颜色。 * * @param outLineWidth 颜色值。 */ // public void setOutLineWidth(@ColorInt int outLineWidth) { public void setOutLineWidth(int outLineWidth) { this.outLineWidth = outLineWidth; invalidate(); } /** * 设置圆形的填充颜色。 * * @param inCircleColor 颜色值。 */ // public void setInCircleColor(@ColorInt int inCircleColor) { public void setInCircleColor(int inCircleColor) { this.inCircleColors = ColorStateList.valueOf(inCircleColor); invalidate(); } /** * 是否需要更新圆的颜色。 */ private void validateCircleColor() { int circleColorTemp = inCircleColors.getColorForState(getDrawableState(), Color.TRANSPARENT); if (circleColor != circleColorTemp) { circleColor = circleColorTemp; invalidate(); } } /** * 设置进度条颜色。 * * @param progressLineColor 颜色值。 */ // public void setProgressColor(@ColorInt int progressLineColor) { public void setProgressColor(int progressLineColor) { this.progressLineColor = progressLineColor; invalidate(); } /** * 设置进度条线的宽度。 * * @param progressLineWidth 宽度值。 */ public void setProgressLineWidth(int progressLineWidth) { this.progressLineWidth = progressLineWidth; invalidate(); } /** * 设置进度。 * * @param progress 进度。 */ public void setProgress(int progress) { this.progress = validateProgress(progress); invalidate(); } /** * 验证进度。 * * @param progress 你要验证的进度值。 * @return 返回真正的进度值。 */ private int validateProgress(int progress) { if (progress > 100) progress = 100; else if (progress < 0) progress = 0; return progress; } /** * 拿到此时的进度。 * * @return 进度值,最大100,最小0。 */ public int getProgress() { return progress; } /** * 设置倒计时总时间。 * * @param timeMillis 毫秒。 */ public void setTimeMillis(long timeMillis) { this.timeMillis = timeMillis; invalidate(); } /** * 拿到进度条计时时间。 * * @return 毫秒。 */ public long getTimeMillis() { return this.timeMillis; } /** * 设置进度条类型。 * * @param progressType {@link ProgressType}. */ public void setProgressType(ProgressType progressType) { this.mProgressType = progressType; resetProgress(); invalidate(); } /** * 重置进度。 */ private void resetProgress() { switch (mProgressType) { case COUNT: progress = 0; break; } } /** * 拿到进度条类型。 * * @return */ public ProgressType getProgressType() { return mProgressType; } /** * 设置进度监听。 * * @param mCountdownProgressListener 监听器。 */ public void setCountdownProgressListener(int what, OnCountdownProgressListener mCountdownProgressListener) { this.listenerWhat = what; this.mCountdownProgressListener = mCountdownProgressListener; } /** * 开始。 */ public void start() { stop(); post(progressChangeTask); } /** * 重新开始。 */ public void reStart() { resetProgress(); start(); } /** * 停止。 */ public void stop() { removeCallbacks(progressChangeTask); } @Override protected void onDraw(Canvas canvas) { // 获取view的边界 getDrawingRect(bounds); int size = bounds.height() > bounds.width() ? bounds.width() : bounds.height(); float outerRadius = size / 2; // 画内部背景 int circleColor = inCircleColors.getColorForState(getDrawableState(), 0); mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(circleColor); canvas.drawCircle(bounds.centerX(), bounds.centerY(), outerRadius - outLineWidth, mPaint); // 画边框圆 mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(outLineWidth); mPaint.setColor(outLineColor); canvas.drawCircle(bounds.centerX(), bounds.centerY(), outerRadius - outLineWidth / 2, mPaint); // 画字 Paint paint = getPaint(); paint.setColor(getCurrentTextColor()); paint.setAntiAlias(true); paint.setTextAlign(Paint.Align.CENTER); float textY = bounds.centerX(); float textS = (bounds.bottom + paint.ascent() / 2) - 10; if (!TextUtils.isEmpty(seconds)) canvas.drawText(seconds, bounds.centerX(), textS, paint); canvas.drawText(getText().toString(), bounds.centerX(), textY, paint); // 画进度条 mPaint.setColor(progressLineColor); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(progressLineWidth); mPaint.setStrokeCap(Paint.Cap.ROUND); int deleteWidth = progressLineWidth + outLineWidth; mArcRect.set(bounds.left + deleteWidth / 2, bounds.top + deleteWidth / 2, bounds.right - deleteWidth / 2, bounds.bottom - deleteWidth / 2); canvas.drawArc(mArcRect, 270, 360 * progress / 100, false, mPaint); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int lineWidth = 4 * (outLineWidth + progressLineWidth); int width = getMeasuredWidth(); int height = getMeasuredHeight(); int size = (width > height ? width : height) + lineWidth; setMeasuredDimension(size, size); } @Override protected void drawableStateChanged() { super.drawableStateChanged(); validateCircleColor(); } /** * 进度更新task。 */ private Runnable progressChangeTask = new Runnable() { @Override public void run() { removeCallbacks(this); switch (mProgressType) { case COUNT: progress += 1; break; } if (progress >= 0 && progress <= 100) { if (mCountdownProgressListener != null) mCountdownProgressListener.onProgress(listenerWhat, progress); invalidate(); postDelayed(progressChangeTask, timeMillis / 100); } else progress = validateProgress(progress); } }; /** * 进度条类型。 */ public enum ProgressType { /** * 顺数进度条,从0-100; */ COUNT, } /** * 进度监听。 */ public interface OnCountdownProgressListener { /** * 进度通知。 * * @param progress 进度值。 */ void onProgress(int what, int progress); } } attrs.xml: <?xml version="1.0" encoding="utf-8"?> <resources> <!-- 欢迎页跳过按钮样式 --> <declare-styleable name="CircleTextProgressbar"> <attr name="in_circle_color" format="color" /> <attr name="cenerTextSie" format="dimension"></attr> <attr name="secondsTextSize" format="dimension"></attr> <attr name="secondsText" format="string"></attr> </declare-styleable> </resources> StartPageActivity.java: package com.demo.startpageskiptimerdemo.activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import com.bumptech.glide.Glide; import com.bumptech.glide.signature.StringSignature; import com.demo.startpageskiptimerdemo.R; import com.demo.startpageskiptimerdemo.widget.CircleTextProgressbar; import java.util.Timer; import java.util.TimerTask; /** * 启动页面 * * @author chenke * @time 2017/12/7 10:13 * @mail [email protected] */ public class StartPageActivity extends AppCompatActivity implements View.OnTouchListener { private final static String TAG = "StartPageActivity"; CircleTextProgressbar mTvSkip; ImageView mStartPageImag; LinearLayout skipLayout; RelativeLayout welBgRlay; // 倒计时timer private Timer timer; // 启动页图片地址 private String imgUrl = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1851053687,4000575540&fm=27&gp=0.jpg"; private int waitTime = 5;// 时长5秒 Handler mHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message message) { switch (message.what) { case 1: gotoHomeActivity(); break; } return false; } }); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_start_page); init(); } private void init() { mTvSkip = (CircleTextProgressbar) findViewById(R.id.tv_red_skip); mStartPageImag = (ImageView) findViewById(R.id.star_page_icon); skipLayout = (LinearLayout) findViewById(R.id.skip_layout); welBgRlay = (RelativeLayout) findViewById(R.id.start_goadvert_lay); mTvSkip.setOutLineColor(Color.TRANSPARENT); mTvSkip.setInCircleColor(Color.parseColor("#AAC6C6C6")); mTvSkip.setProgressColor(Color.WHITE); mTvSkip.setProgressLineWidth(5); // 开始时计 mCountDowntimer(); mTvSkip.reStart(); // 设置skipLayout浮在整个页面的最上层 welBgRlay.bringChildToFront(skipLayout); mTvSkip.setOnTouchListener(this); mStartPageImag.setOnTouchListener(this); // 加载图片 if (!TextUtils.isEmpty(imgUrl)) { Glide.with(this).load(imgUrl).signature(new StringSignature("1")).into (mStartPageImag); } } @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { if (null != timer) { timer.cancel(); timer = null; } switch (view.getId()) { case R.id.star_page_icon: // 广告 //********** 跳转网页 (进入启动页面广告详情)*********** break; case R.id.tv_red_skip: // 跳转计时view if (null != timer) { timer.cancel(); timer = null; } mHandler.sendEmptyMessage(1); return true; } } return false; } /** * 进入首页 */ public void gotoHomeActivity() { Intent homeIntent = new Intent(); homeIntent.setClass(this, MainActivity.class); startActivity(homeIntent); finish(); } /** * 计时器 */ private void mCountDowntimer() { timer = new Timer(); mTvSkip.setSeconds(waitTime + "s"); timer.schedule(task, 1000, 1000); } /** * 计时器线程 */ TimerTask task = new TimerTask() { @Override public void run() { runOnUiThread(new Runnable() { // UI thread @Override public void run() { waitTime--; mTvSkip.setSeconds(waitTime + "s"); if (waitTime <= 0) { if (null != timer) { timer.cancel(); timer = null; } mHandler.sendEmptyMessage(1); } } }); } }; } activity_start_page.xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/start_goadvert_lay" android:layout_width="match_parent" android:layout_height="match_parent" android: <ImageView android:id="@+id/star_page_icon" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:scaleType="fitXY" /> <LinearLayout android:id="@+id/skip_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:gravity="center" android:orientation="horizontal" android:paddingRight="10dp" android:paddingTop="10dp" android:visibility="visible"> <com.demo.startpageskiptimerdemo.widget.CircleTextProgressbar android:id="@+id/tv_red_skip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳过" android:textColor="@android:color/white" android:textSize="10dp" /> </LinearLayout> </RelativeLayout> styles.xml: <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <!--启动页面跳过按钮自定义style--> <style name="CustomProgressStyle" parent="@android:style/Widget.ProgressBar.Large"> <item name="android:minWidth">35dip</item> <item name="android:maxWidth">35dip</item> <item name="android:minHeight">35dip</item> <item name="android:maxHeight">35dip</item> </style> </resources> AndroidManifest.xml中添加请求网络权限: <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> build.gradle中dependencies添加: // 异步加载图片 compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.android.support:support-v4:23.3.0'

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值