CustomBanner
package com.dash.a03_custom_view_02.view;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
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 com.bumptech.glide.Glide;
import com.dash.a03_custom_view_02.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Dash on 2017/12/29.
*/
public class CustomBanner extends FrameLayout {
private ViewPager viewPager;
private LinearLayout linearLayout;
private List<String> list;
private int time = 2;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what == 0){
int currentItem = viewPager.getCurrentItem();
viewPager.setCurrentItem(currentItem +1);
//再次发送
sendEmptyMessageDelayed(0,time*1000);
}
}
};
private List<ImageView> listDoc;
private OnClickLisner onClickLisner;
public CustomBanner(@NonNull Context context) {
super(context);
init();
}
public CustomBanner(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomBanner(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
/**
* 初始化
*/
private void init() {
View view = View.inflate(getContext(), R.layout.bannner_layout,this);
//找到控件
viewPager = view.findViewById(R.id.banner_view_pager);
linearLayout = view.findViewById(R.id.linear_bannner);
}
/**
* 对外提供设置image路径的方法
*/
public void setImageUrls(List<String> list){
this.list = list;
if (list == null){
return;
}
//设置适配器
LunBoAdapter lunBoAdapter = new LunBoAdapter(getContext(), list);
viewPager.setAdapter(lunBoAdapter);
initDoc();
//显示中间某个位置
viewPager.setCurrentItem(list.size()*10000);
//使用handler自动轮播
handler.sendEmptyMessageDelayed(0,time*1000);
//状态改变的监听事件
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<listDoc.size();i++){
if (position%listDoc.size() == i){
listDoc.get(i).setBackgroundResource(R.drawable.shape_01);
}else {
listDoc.get(i).setBackgroundResource(R.drawable.shape_02);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
/**
* 初始化小圆点
*/
private void initDoc() {
//创建一个集合,记录这些小圆点
listDoc = new ArrayList<>();
//清空布局
linearLayout.removeAllViews();
for (int i=0;i<list.size();i++){
ImageView docImage = new ImageView(getContext());
if (i == 0){
docImage.setBackgroundResource(R.drawable.shape_01);
}else {
docImage.setBackgroundResource(R.drawable.shape_02);
}
//添加到集合
listDoc.add(docImage);
//添加到线性布局
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5,0,5,0);
linearLayout.addView(docImage,params);
}
}
/**
* 对外提供轮播的时间
*/
public void setTimeSecond(int time){
this.time = time;
}
/**
* 点击事件
* @param onClickLisner
*/
public void setClickListner(OnClickLisner onClickLisner) {
this.onClickLisner = onClickLisner;
}
private class LunBoAdapter extends PagerAdapter{
private List<String> list;
private Context context;
public LunBoAdapter(Context context, List<String> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
//创建imageView
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//加载这张图片
Glide.with(context).load(list.get(position%list.size())).into(imageView);
//点击事件
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//触发
onClickLisner.onItemClick(position%list.size());
}
});
//触摸事件
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
//取消handler身上的消息和回调
handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_MOVE:
handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_CANCEL:
handler.sendEmptyMessageDelayed(0,time*1000);
break;
case MotionEvent.ACTION_UP:
handler.sendEmptyMessageDelayed(0,time*1000);
break;
}
return false;
}
});
//添加到容器
container.addView(imageView);
//返回
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
public interface OnClickLisner{
void onItemClick(int position);
}
}
OkHttp3Util
package com.dash.a03_custom_view_02.util;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* Created by Dash on 2017/12/8.
*/
public class OkHttp3Util {
/**
* 懒汉 安全 加同步
* 1.私有的静态成员变量 只声明不创建
* 2.私有的构造方法
* 3.提供返回实例的静态方法
*/
private static OkHttpClient okHttpClient = null;
private OkHttp3Util() {
}
public static OkHttpClient getInstance() {
if (okHttpClient == null) {
//加同步安全
synchronized (OkHttp3Util.class) {
if (okHttpClient == null) {
//okhttp可以缓存数据....指定缓存路径
File sdcache = new File(Environment.getExternalStorageDirectory(), "cache");
//指定缓存大小
int cacheSize = 10 * 1024 * 1024;
okHttpClient = new OkHttpClient.Builder()//构建器
.connectTimeout(15, TimeUnit.SECONDS)//连接超时
.writeTimeout(20, TimeUnit.SECONDS)//写入超时
.readTimeout(20, TimeUnit.SECONDS)//读取超时
.cache(new Cache(sdcache.getAbsoluteFile(), cacheSize))//设置缓存
.build();
}
}
}
return okHttpClient;
}
/**
* get请求
* 参数1 url
* 参数2 回调Callback
*/
public static void doGet(String oldUrl, Callback callback) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getInstance();
//创建Request
Request request = new Request.Builder().url(oldUrl).build();
//得到Call对象
Call call = okHttpClient.newCall(request);
//执行异步请求
call.enqueue(callback);
}
/**
* post请求
* 参数1 url
* 参数2 Map<String, String> params post请求的时候给服务器传的数据
* add..("","")
* add()
*/
public static void doPost(String url, Map<String, String> params, Callback callback) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getInstance();
//3.x版本post请求换成FormBody 封装键值对参数
FormBody.Builder builder = new FormBody.Builder();
//遍历集合,,,map集合遍历方式
for (String key : params.keySet()) {
builder.add(key, params.get(key));
}
//创建Request....formBody...new formBody.Builder()...add()....build()
Request request = new Request.Builder().url(url).post(builder.build()).build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
/**
* post请求上传文件....包括图片....流的形式传任意文件...
* 参数1 url
* file表示上传的文件
* fileName....文件的名字,,例如aaa.jpg
* params ....传递除了file文件 其他的参数放到map集合
*
*/
public static void uploadFile(String url, File file, String fileName,Map<String,String> params,Callback callback) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getInstance();
//MultipartBody多功能的请求实体对象,,,formBody只能传表单形式的数据
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
//参数
if (params != null){
for (String key : params.keySet()){
builder.addFormDataPart(key,params.get(key));
}
}
//文件...参数name指的是请求路径中所接受的参数...如果路径接收参数键值是fileeeee,此处应该改变
builder.addFormDataPart("file",fileName,RequestBody.create(MediaType.parse("application/octet-stream"),file));
//构建
MultipartBody multipartBody = builder.build();
//创建Request
Request request = new Request.Builder().url(url).post(multipartBody).build();
//得到Call
Call call = okHttpClient.newCall(request);
//执行请求
call.enqueue(callback);
}
/**
* Post请求发送JSON数据....{"name":"zhangsan","pwd":"123456"}
* 参数一:请求Url
* 参数二:请求的JSON
* 参数三:请求回调
*/
public static void doPostJson(String url, String jsonParams, Callback callback) {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonParams);
Request request = new Request.Builder().url(url).post(requestBody).build();
Call call = getInstance().newCall(request);
call.enqueue(callback);
}
/**
* 下载文件 以流的形式把apk写入的指定文件 得到file后进行安装
* 参数er:请求Url
* 参数san:保存文件的文件夹....download
*/
public static void download(final Activity context, final String url, final String saveDir) {
Request request = new Request.Builder().url(url).build();
Call call = getInstance().newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//com.orhanobut.logger.Logger.e(e.getLocalizedMessage());
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
try {
is = response.body().byteStream();//以字节流的形式拿回响应实体内容
//apk保存路径
final String fileDir = isExistDir(saveDir);
//文件
File file = new File(fileDir, getNameFromUrl(url));
fos = new FileOutputStream(file);
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.flush();
context.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "下载成功:" + fileDir + "," + getNameFromUrl(url), Toast.LENGTH_SHORT).show();
}
});
//apk下载完成后 调用系统的安装方法
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
context.startActivity(intent);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) is.close();
if (fos != null) fos.close();
}
}
});
}
/**
* 判断下载目录是否存在......并返回绝对路径
*
* @param saveDir
* @return
* @throws IOException
*/
public static String isExistDir(String saveDir) throws IOException {
// 下载位置
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File downloadFile = new File(Environment.getExternalStorageDirectory(), saveDir);
if (!downloadFile.mkdirs()) {
downloadFile.createNewFile();
}
String savePath = downloadFile.getAbsolutePath();
Log.e("savePath", savePath);
return savePath;
}
return null;
}
/**
* @param url
* @return 从下载连接中解析出文件名
*/
private static String getNameFromUrl(String url) {
return url.substring(url.lastIndexOf("/") + 1);
}
}
main、、
package com.dash.a03_custom_view_02;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.dash.a03_custom_view_02.bean.DetalBean;
import com.dash.a03_custom_view_02.util.OkHttp3Util;
import com.dash.a03_custom_view_02.view.CombineView;
import com.dash.a03_custom_view_02.view.CustomBanner;
import com.dash.a03_custom_view_02.view.SwitchButton;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private CustomBanner customBanner;
private SwitchButton switchButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*CombineView combineView = findViewById(R.id.combine_view);
combineView.setText("是否开启夜间模式");
combineView.setChecked(true);*/
/*customBanner = findViewById(R.id.custom_banner);
//请求数据进行解析展示
getDataFromNet();*/
switchButton = findViewById(R.id.switch_button);
switchButton.setBackImage(R.drawable.switch_background);
switchButton.setSlideImage(R.drawable.slide_button_background);
switchButton.setState(false);
}
private void getDataFromNet() {
OkHttp3Util.doGet("https://www.zhaoapi.cn/product/getProductDetail?Pid=1&source=android", new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
String json = response.body().string();
final DetalBean detalBean = new Gson().fromJson(json,DetalBean.class);
final List<String> list = new ArrayList<>();
String[] images = detalBean.getData().getImages().split("\\|");
for (int i = 0;i<images.length;i++){
list.add(images[i]);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
//设置时间
customBanner.setTimeSecond(5);
//设置显示轮播
customBanner.setImageUrls(list);
//设置点击事件
customBanner.setClickListner(new CustomBanner.OnClickLisner() {
@Override
public void onItemClick(int position) {
Toast.makeText(MainActivity.this,"点击",Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
});
}
}
main布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dash="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dash.a03_custom_view_02.MainActivity">
<!--<com.dash.a03_custom_view_02.view.CountView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />-->
<!-- <com.dash.a03_custom_view_02.view.CombineView
android:id="@+id/combine_view"
dash:text="是否选择这个美女"
dash:checked="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.dash.a03_custom_view_02.view.CombineView>-->
<!--<com.dash.a03_custom_view_02.view.CustomBanner
android:id="@+id/custom_banner"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.dash.a03_custom_view_02.view.CustomBanner>-->
<com.dash.a03_custom_view_02.view.SwitchButton
android:id="@+id/switch_button"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
banner——layout'
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/banner_view_pager"
android:layout_width="match_parent"
android:layout_height="200dp">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/linear_bannner"
android:layout_centerHorizontal="true"
android:layout_alignBottom="@+id/banner_view_pager"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</RelativeLayout>
combine——layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_gravity="center_vertical"
android:id="@+id/combine_text"
android:text="hahhaha"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<CheckBox
android:layout_gravity="center_vertical"
android:id="@+id/combine_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dash.a03_custom_view_02">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
依赖
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.dash.a03_custom_view_02"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
//okhttp
compile 'com.squareup.okhttp3:okhttp:3.6.0'
compile 'com.squareup.okio:okio:1.11.0'
//gson
compile 'com.google.code.gson:gson:2.8.2'
//glide
implementation 'com.github.bumptech.glide:glide:4.4.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.4.0'
}