jimu-decompiled/sources/com/youth/banner/Banner.java
2025-05-13 19:24:51 +02:00

564 lines
18 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.youth.banner;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.youth.banner.listener.OnBannerClickListener;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoaderInterface;
import com.youth.banner.view.BannerViewPager;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/* loaded from: classes2.dex */
public class Banner extends FrameLayout implements ViewPager.OnPageChangeListener {
private List<View> A;
private List<ImageView> B;
private Context C;
private BannerViewPager D;
private TextView E;
private TextView F;
private TextView G;
private LinearLayout H;
private LinearLayout I;
private LinearLayout J;
private ImageView K;
private ImageLoaderInterface L;
private BannerPagerAdapter M;
private ViewPager.OnPageChangeListener N;
private BannerScroller O;
private OnBannerClickListener P;
private OnBannerListener Q;
private DisplayMetrics R;
private WeakHandler S;
private final Runnable T;
public String a;
private int b;
private int c;
private int d;
private int e;
private int f;
private int g;
private int h;
private int i;
private boolean j;
private boolean k;
private int l;
private int m;
private int n;
private int o;
private int p;
private int q;
private int r;
private int s;
private int t;
private int u;
private int v;
private int x;
private List<String> y;
private List z;
class BannerPagerAdapter extends PagerAdapter {
BannerPagerAdapter() {
}
@Override // androidx.viewpager.widget.PagerAdapter
public void destroyItem(ViewGroup viewGroup, int i, Object obj) {
viewGroup.removeView((View) obj);
}
@Override // androidx.viewpager.widget.PagerAdapter
public int getCount() {
return Banner.this.A.size();
}
@Override // androidx.viewpager.widget.PagerAdapter
public Object instantiateItem(ViewGroup viewGroup, final int i) {
viewGroup.addView((View) Banner.this.A.get(i));
View view = (View) Banner.this.A.get(i);
if (Banner.this.P != null) {
view.setOnClickListener(new View.OnClickListener() { // from class: com.youth.banner.Banner.BannerPagerAdapter.1
@Override // android.view.View.OnClickListener
public void onClick(View view2) {
Log.e(Banner.this.a, "你正在使用旧版点击事件接口下标是从1开始为了体验请更换为setOnBannerListener下标从0开始计算");
Banner.this.P.OnBannerClick(i);
}
});
}
if (Banner.this.Q != null) {
view.setOnClickListener(new View.OnClickListener() { // from class: com.youth.banner.Banner.BannerPagerAdapter.2
@Override // android.view.View.OnClickListener
public void onClick(View view2) {
Banner.this.Q.OnBannerClick(Banner.this.c(i));
}
});
}
return view;
}
@Override // androidx.viewpager.widget.PagerAdapter
public boolean isViewFromObject(View view, Object obj) {
return view == obj;
}
}
public Banner(Context context) {
this(context, null);
}
private void setImageList(List<?> list) {
if (list == null || list.size() <= 0) {
this.K.setVisibility(0);
Log.e(this.a, "The image data set is empty.");
return;
}
this.K.setVisibility(8);
e();
int i = 0;
while (i <= this.s + 1) {
ImageLoaderInterface imageLoaderInterface = this.L;
View createImageView = imageLoaderInterface != null ? imageLoaderInterface.createImageView(this.C) : null;
if (createImageView == null) {
createImageView = new ImageView(this.C);
}
setScaleType(createImageView);
Object obj = i == 0 ? list.get(this.s - 1) : i == this.s + 1 ? list.get(0) : list.get(i - 1);
this.A.add(createImageView);
ImageLoaderInterface imageLoaderInterface2 = this.L;
if (imageLoaderInterface2 != null) {
imageLoaderInterface2.displayImage(this.C, obj, createImageView);
} else {
Log.e(this.a, "Please set images loader.");
}
i++;
}
}
private void setScaleType(View view) {
if (view instanceof ImageView) {
ImageView imageView = (ImageView) view;
switch (this.x) {
case 0:
imageView.setScaleType(ImageView.ScaleType.CENTER);
break;
case 1:
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
break;
case 2:
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
break;
case 3:
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
break;
case 4:
imageView.setScaleType(ImageView.ScaleType.FIT_END);
break;
case 5:
imageView.setScaleType(ImageView.ScaleType.FIT_START);
break;
case 6:
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
break;
case 7:
imageView.setScaleType(ImageView.ScaleType.MATRIX);
break;
}
}
}
@Override // android.view.ViewGroup, android.view.View
public boolean dispatchTouchEvent(MotionEvent motionEvent) {
if (this.j) {
int action = motionEvent.getAction();
if (action == 1 || action == 3 || action == 4) {
b();
} else if (action == 0) {
c();
}
}
return super.dispatchTouchEvent(motionEvent);
}
@Override // androidx.viewpager.widget.ViewPager.OnPageChangeListener
public void onPageScrollStateChanged(int i) {
ViewPager.OnPageChangeListener onPageChangeListener = this.N;
if (onPageChangeListener != null) {
onPageChangeListener.onPageScrollStateChanged(i);
}
if (i == 0) {
int i2 = this.t;
if (i2 == 0) {
this.D.a(this.s, false);
return;
} else {
if (i2 == this.s + 1) {
this.D.a(1, false);
return;
}
return;
}
}
if (i != 1) {
return;
}
int i3 = this.t;
int i4 = this.s;
if (i3 == i4 + 1) {
this.D.a(1, false);
} else if (i3 == 0) {
this.D.a(i4, false);
}
}
@Override // androidx.viewpager.widget.ViewPager.OnPageChangeListener
public void onPageScrolled(int i, float f, int i2) {
ViewPager.OnPageChangeListener onPageChangeListener = this.N;
if (onPageChangeListener != null) {
onPageChangeListener.onPageScrolled(c(i), f, i2);
}
}
@Override // androidx.viewpager.widget.ViewPager.OnPageChangeListener
public void onPageSelected(int i) {
this.t = i;
ViewPager.OnPageChangeListener onPageChangeListener = this.N;
if (onPageChangeListener != null) {
onPageChangeListener.onPageSelected(c(i));
}
int i2 = this.g;
if (i2 == 1 || i2 == 4 || i2 == 5) {
List<ImageView> list = this.B;
int i3 = this.v - 1;
int i4 = this.s;
list.get((i3 + i4) % i4).setImageResource(this.m);
List<ImageView> list2 = this.B;
int i5 = this.s;
list2.get(((i - 1) + i5) % i5).setImageResource(this.l);
this.v = i;
}
if (i == 0) {
i = this.s;
}
if (i > this.s) {
i = 1;
}
int i6 = this.g;
if (i6 != 1) {
if (i6 == 2) {
this.G.setText(i + "/" + this.s);
return;
}
if (i6 != 3) {
if (i6 == 4) {
this.E.setText(this.y.get(i - 1));
return;
} else {
if (i6 != 5) {
return;
}
this.E.setText(this.y.get(i - 1));
return;
}
}
this.F.setText(i + "/" + this.s);
this.E.setText(this.y.get(i - 1));
}
}
public void setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener) {
this.N = onPageChangeListener;
}
public Banner(Context context, AttributeSet attributeSet) {
this(context, attributeSet, 0);
}
private void b(Context context, AttributeSet attributeSet) {
this.A.clear();
a(context, attributeSet);
View inflate = LayoutInflater.from(context).inflate(this.n, (ViewGroup) this, true);
this.K = (ImageView) inflate.findViewById(R$id.bannerDefaultImage);
this.D = (BannerViewPager) inflate.findViewById(R$id.bannerViewPager);
this.J = (LinearLayout) inflate.findViewById(R$id.titleView);
this.H = (LinearLayout) inflate.findViewById(R$id.circleIndicator);
this.I = (LinearLayout) inflate.findViewById(R$id.indicatorInside);
this.E = (TextView) inflate.findViewById(R$id.bannerTitle);
this.G = (TextView) inflate.findViewById(R$id.numIndicator);
this.F = (TextView) inflate.findViewById(R$id.numIndicatorInside);
this.K.setImageResource(this.f);
f();
}
private void d() {
this.B.clear();
this.H.removeAllViews();
this.I.removeAllViews();
for (int i = 0; i < this.s; i++) {
ImageView imageView = new ImageView(this.C);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(this.c, this.d);
int i2 = this.b;
layoutParams.leftMargin = i2;
layoutParams.rightMargin = i2;
if (i == 0) {
imageView.setImageResource(this.l);
} else {
imageView.setImageResource(this.m);
}
this.B.add(imageView);
int i3 = this.g;
if (i3 == 1 || i3 == 4) {
this.H.addView(imageView, layoutParams);
} else if (i3 == 5) {
this.I.addView(imageView, layoutParams);
}
}
}
private void e() {
this.A.clear();
int i = this.g;
if (i == 1 || i == 4 || i == 5) {
d();
return;
}
if (i == 3) {
this.F.setText("1/" + this.s);
return;
}
if (i == 2) {
this.G.setText("1/" + this.s);
}
}
private void f() {
try {
Field declaredField = ViewPager.class.getDeclaredField("j");
declaredField.setAccessible(true);
this.O = new BannerScroller(this.D.getContext());
this.O.a(this.i);
declaredField.set(this.D, this.O);
} catch (Exception e) {
Log.e(this.a, e.getMessage());
}
}
private void g() {
int i = this.s > 1 ? 0 : 8;
int i2 = this.g;
if (i2 == 1) {
this.H.setVisibility(i);
return;
}
if (i2 == 2) {
this.G.setVisibility(i);
return;
}
if (i2 == 3) {
this.F.setVisibility(i);
i();
} else if (i2 == 4) {
this.H.setVisibility(i);
i();
} else {
if (i2 != 5) {
return;
}
this.I.setVisibility(i);
i();
}
}
private void h() {
this.t = 1;
if (this.M == null) {
this.M = new BannerPagerAdapter();
this.D.a((ViewPager.OnPageChangeListener) this);
}
this.D.setAdapter(this.M);
this.D.setFocusable(true);
this.D.setCurrentItem(1);
int i = this.u;
if (i != -1) {
this.H.setGravity(i);
}
if (!this.k || this.s <= 1) {
this.D.setScrollable(false);
} else {
this.D.setScrollable(true);
}
if (this.j) {
b();
}
}
private void i() {
if (this.y.size() != this.z.size()) {
throw new RuntimeException("[Banner] --> The number of titles and images is different");
}
int i = this.p;
if (i != -1) {
this.J.setBackgroundColor(i);
}
int i2 = this.o;
if (i2 != -1) {
this.J.setLayoutParams(new RelativeLayout.LayoutParams(-1, i2));
}
int i3 = this.q;
if (i3 != -1) {
this.E.setTextColor(i3);
}
int i4 = this.r;
if (i4 != -1) {
this.E.setTextSize(0, i4);
}
List<String> list = this.y;
if (list == null || list.size() <= 0) {
return;
}
this.E.setText(this.y.get(0));
this.E.setVisibility(0);
this.J.setVisibility(0);
}
public void c() {
this.S.b(this.T);
}
public Banner(Context context, AttributeSet attributeSet, int i) {
super(context, attributeSet, i);
this.a = "banner";
this.b = 5;
this.g = 1;
this.h = 2000;
this.i = 800;
this.j = true;
this.k = true;
this.l = R$drawable.gray_radius;
this.m = R$drawable.white_radius;
this.n = R$layout.banner;
this.s = 0;
this.u = -1;
this.v = 1;
this.x = 1;
this.S = new WeakHandler();
this.T = new Runnable() { // from class: com.youth.banner.Banner.1
@Override // java.lang.Runnable
public void run() {
if (Banner.this.s <= 1 || !Banner.this.j) {
return;
}
Banner banner = Banner.this;
banner.t = (banner.t % (Banner.this.s + 1)) + 1;
if (Banner.this.t == 1) {
Banner.this.D.a(Banner.this.t, false);
Banner.this.S.a(Banner.this.T);
} else {
Banner.this.D.setCurrentItem(Banner.this.t);
Banner.this.S.a(Banner.this.T, Banner.this.h);
}
}
};
this.C = context;
this.y = new ArrayList();
this.z = new ArrayList();
this.A = new ArrayList();
this.B = new ArrayList();
this.R = context.getResources().getDisplayMetrics();
this.e = this.R.widthPixels / 80;
b(context, attributeSet);
}
private void a(Context context, AttributeSet attributeSet) {
if (attributeSet == null) {
return;
}
TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R$styleable.Banner);
this.c = obtainStyledAttributes.getDimensionPixelSize(R$styleable.Banner_indicator_width, this.e);
this.d = obtainStyledAttributes.getDimensionPixelSize(R$styleable.Banner_indicator_height, this.e);
this.b = obtainStyledAttributes.getDimensionPixelSize(R$styleable.Banner_indicator_margin, 5);
this.l = obtainStyledAttributes.getResourceId(R$styleable.Banner_indicator_drawable_selected, R$drawable.gray_radius);
this.m = obtainStyledAttributes.getResourceId(R$styleable.Banner_indicator_drawable_unselected, R$drawable.white_radius);
this.x = obtainStyledAttributes.getInt(R$styleable.Banner_image_scale_type, this.x);
this.h = obtainStyledAttributes.getInt(R$styleable.Banner_delay_time, 2000);
this.i = obtainStyledAttributes.getInt(R$styleable.Banner_scroll_time, 800);
this.j = obtainStyledAttributes.getBoolean(R$styleable.Banner_is_auto_play, true);
this.p = obtainStyledAttributes.getColor(R$styleable.Banner_title_background, -1);
this.o = obtainStyledAttributes.getDimensionPixelSize(R$styleable.Banner_title_height, -1);
this.q = obtainStyledAttributes.getColor(R$styleable.Banner_title_textcolor, -1);
this.r = obtainStyledAttributes.getDimensionPixelSize(R$styleable.Banner_title_textsize, -1);
this.n = obtainStyledAttributes.getResourceId(R$styleable.Banner_banner_layout, this.n);
this.f = obtainStyledAttributes.getResourceId(R$styleable.Banner_banner_default_image, R$drawable.no_banner);
obtainStyledAttributes.recycle();
}
public int c(int i) {
int i2 = this.s;
int i3 = (i - 1) % i2;
return i3 < 0 ? i3 + i2 : i3;
}
public Banner b(int i) {
if (i == 5) {
this.u = 19;
} else if (i == 6) {
this.u = 17;
} else if (i == 7) {
this.u = 21;
}
return this;
}
public Banner b(List<?> list) {
this.z = list;
this.s = list.size();
return this;
}
public Banner a(ImageLoaderInterface imageLoaderInterface) {
this.L = imageLoaderInterface;
return this;
}
public void b() {
this.S.b(this.T);
this.S.a(this.T, this.h);
}
public Banner a(List<String> list) {
this.y = list;
return this;
}
public Banner a(int i) {
this.g = i;
return this;
}
public Banner a() {
g();
setImageList(this.z);
h();
return this;
}
public Banner a(OnBannerListener onBannerListener) {
this.Q = onBannerListener;
return this;
}
}