XBanner轮播图控件的使用(我又来了,哈哈哈)
上次的百度地图SDK采用的方法是直接copy了jar文件(可以说是学习了如何使用jar包)
这次的重点是 maven 的使用这个操作只是一种方法,因为其他的我暂时还不会,大佬勿喷
先说说轮播图控件,就是软件内一个不断自动切换图片的一个控件
懒的下载了,直接浏览器看吧(就像京东这个1元问医生的图片,他会自己不断切换,按照开发者自己设置的不断更新)
我的实现效果(图片很熟悉吧,我CSDN的头像,为啥不用别的,因为我懒)
下面说怎么操作
这是我使用的一个XBanner控件的官网https://www.freesion.com/article/6258916864/ ,自取
直接采用第一种方法,因为它可以学习maven
其实对于大多小白来说不会使用的就是上面第二个代码框内的代码,都错误的把他添加到总工程项目的build.gradle里(为啥这么说,因为刚开始我就是这样做的,哼! )
plugins {
id 'com.android.application' version '4.2.2' apply false
id 'com.android.library' version '4.2.2' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
所以要怎么操作呢??? 它需要添加到总项目的settings.gradle里使用!!!(对于我一个小白来说,maven的使用就是这样 )
为啥有的项目里不能显示我左边那样的tree(就是列表),
因为需要转化到Project模式下开发(如图)
添加maven库
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
rootProject.name = "XBan"
include ':app'
Add the dependency(添加依赖)
//将latestVersion替换成上面 jitpack 后面的版本号这句话是要你把github上最新的版本添加过来!!!
dependencies {
implementation 'com.github.xiaohaibin:XBanner:latestVersion'
}
我这里使用的是1.8.9 版本
implementation 'com.github.xiaohaibin:XBanner:1.8.9'
implementation 'com.github.bumptech.glide:glide:4.9.0'
在app目录下,别搞错了
添加xml
<com.stx.xhb.xbanner.XBanner
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/xbanner"
android:layout_width="match_parent"
android:layout_height="180dp"
app:AutoPlayTime="3000"
app:pointsContainerBackground="#44aaaaaa"
app:pointNormal="@drawable/shape_noraml"
app:pointSelect="@drawable/shape_selected"
app:pointsPosition="RIGHT"
app:tipTextSize="12sp"
app:isShowNumberIndicator="true"
app:isShowIndicatorOnlyOne="true"
app:pageChangeDuration="800"/>
添加java声明
XBanner banner = (XBanner) findViewById(R.id.xbanner);
插入图片(为啥是List,因为banner.setData需要数组,我只使用了第一个参数
List<String> list2 = new ArrayList<>();
for (int i = 0; i < 7; i++) {
list2.add("https://profile.csdnimg.cn/F/9/E/1_qq_37567841");
}
banner.setData(list2, null);
banner.loadImage(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, Object model, View view, int position) {
RoundedCorners roundedCorners = new RoundedCorners(20);
RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
Glide.with(MainActivity.this).load(list2.get(position)).apply(options).into((ImageView) view);
}
});
banner.setPageTransformer(Transformer.Default);
完整的java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.stx.xhb.xbanner.XBanner;
import com.stx.xhb.xbanner.transformers.Transformer;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
XBanner banner = (XBanner) findViewById(R.id.xbanner);
List<String> list2 = new ArrayList<>();
for (int i = 0; i < 7; i++) {
list2.add("https://profile.csdnimg.cn/F/9/E/1_qq_37567841");
}
banner.setData(list2, null);
banner.loadImage(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, Object model, View view, int position) {
RoundedCorners roundedCorners = new RoundedCorners(20);
RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
Glide.with(MainActivity.this).load(list2.get(position)).apply(options).into((ImageView) view);
}
});
banner.setPageTransformer(Transformer.Default);
}
}
运行看看
是不是报错了,哈哈哈
Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.IResultReceiver found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.IResultReceiver$Stub found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.ResultReceiver found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.ResultReceiver$1 found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Duplicate class android.support.v4.os.ResultReceiver$MyRunnable found in modules core-1.5.0-runtime (androidx.core:core:1.5.0) and support-compat-27.1.1-runtime (com.android.support:support-compat:27.1.1)
Go to the documentation to learn how to Fix dependency resolution errors.
因为这些依赖大多是-混合支持库,所以怎么解决呢?
在总项目 gradle.properties里添加下面的代码就行了重复的自己删除一个
# 需要在gradle.properties中添加下面两行代码
# 这是因为混合支持库。通过添加这些行选择androidX作为您的支持库
android.useAndroidX=true
android.enableJetifier=true
再运行一下试试,可以了吧
快,咱也不要投币,抬抬您的贵手给咱点个赞吧,写个评论也行啊
优化一下,添加生命周期、与Activity生命周期联动
当Activity失去焦点时立即停止自动轮播;当Activity获得焦点时才开始自动轮播。代码如下:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.stx.xhb.xbanner.XBanner;
import com.stx.xhb.xbanner.transformers.Transformer;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
XBanner banner ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
banner = (XBanner) findViewById(R.id.xbanner);
List<String> list2 = new ArrayList<>();
for (int i = 0; i < 7; i++) {
list2.add("https://profile.csdnimg.cn/F/9/E/1_qq_37567841");
}
banner.setData(list2, null);
banner.loadImage(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, Object model, View view, int position) {
RoundedCorners roundedCorners = new RoundedCorners(20);
RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
Glide.with(MainActivity.this).load(list2.get(position)).apply(options).into((ImageView) view);
}
});
banner.setPageTransformer(Transformer.Default);
}
@Override
protected void onResume() {
super.onResume();
banner.startAutoPlay();
}
@Override
protected void onStop() {
super.onStop();
banner.stopAutoPlay();
}
}
教程里 不会的地方就写个评论,我会尽里帮忙解决的
|