友盟+高德+XRecyclerView+shap动画+头像

https://github.com/LGQ1001/Zk3_lx01

1.导包

compileSdkVersion 27
defaultConfig {
    applicationId "com.bwie.main.zk3_lx01"
    minSdkVersion 15
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    //导入so库
    ndk {
        abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86", "arm64-v8a"
    }
}
    //okhttp
    implementation 'com.squareup.okhttp3:okhttp:3.11.0'
    //okhttp的log信息
    implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
    //gson
    implementation 'com.google.code.gson:gson:2.8.5'
    //glide
    implementation 'com.github.bumptech.glide:glide:4.7.1'
    //banner
    implementation 'com.youth.banner:banner:1.4.9'
    //xrecyclerview
    implementation 'com.jcodecraeer:xrecyclerview:1.5.9'
    //友盟
    implementation 'com.umeng.sdk:common:latest.integration'
    //高德地图+定位
    implementation 'com.amap.api:3dmap:latest.integration'
    implementation 'com.amap.api:location:latest.integration'
}
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '27.1.1'
            }
        }
    }
}

2.依赖

<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" />
<!-- //友盟 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- 头像相册 -->
<uses-permission android:name="android.permission.CAMERA" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<uses-permission android:name="android.permission.VIBRATE" />
<!-- 高德 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application
    android:name=".App"

3.实现布局效果,(友盟第三方不加了,太多)

activity_di_tu.xml

<com.amap.api.maps.MapView
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

activity_gereng.xml

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="个人中心"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="头像"/>

    <ImageView
        android:id="@+id/img"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher_background"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <Button
        android:onClick="xiangce"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:text="打开相册并裁剪"
        />

    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:onClick="paizhao"
        android:text="拍照并裁剪" />

</LinearLayout>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="tolistgo"
    android:text="列表展示" />

activity_main.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="8">

    <ImageView
        android:id="@+id/img_0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/shap_values"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <Button
        android:onClick="btn_1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="渐变"/>

    <Button
        android:onClick="btn_2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="旋转"/>

    <Button
        android:onClick="btn_3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="平移"/>

    <Button
        android:onClick="btn_4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="缩放"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <Button
        android:onClick="btn_5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="VALUEANMATOR平移"/>

</LinearLayout>

activity_qqloign.xml

<Button
    android:onClick="clik_QQview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="点击QQ登录"/>

activity_to_list_go.xml

<com.jcodecraeer.xrecyclerview.XRecyclerView
    android:id="@+id/Xrv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

xrv_layout.xml

<ImageView
    android:id="@+id/iv"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@mipmap/ic_launcher" />

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

shap_values.xml

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="oval"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <size android:width="66dp"
          android:height="66dp"/>
    <solid android:color="@color/colorAccent"/>


</shape>

代码页面

MyAdapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private Context context;
    private ShowBean showBean;
    private List<String> list;
    private OnItemClickListener onItemClickListener;

    public MyAdapter(Context context, ShowBean showBean) {
        this.context = context;
        this.showBean = showBean;
        list = new ArrayList<>();
        for (ShowBean.DataBean dataBean : showBean.data) {
            String[] split = dataBean.images.split("\\|");
            list.add(split[0]);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.xrv_layout, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
              holder.tv.setText(showBean.data.get(position).title);
              Glide.with(context).load(list.get(position)).into(holder.iv);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null){
                    onItemClickListener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv;
        private TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.iv);
            tv = itemView.findViewById(R.id.tv);
        }
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}

ShowBean

public class ShowBean {
    public String msg;
    public String code;
    public List<DataBean> data;
    public class DataBean{
        public String images;
        public String title;
    }
}

OkHttpUtils

public class OkHttpUtils {
    private static OkHttpUtils okHttpUtils;
    private OkHttpClient okHttpClient;    
    //构造方法私有的?因为不能被调用者new的对象,只能给自己new
    private OkHttpUtils() {
        okHttpClient = new OkHttpClient.Builder()
                .writeTimeout(2000, TimeUnit.MICROSECONDS)
                .build();
    }
    //public 暴露给调用者,双重检验锁
    public static OkHttpUtils getInstance() {
        if (okHttpUtils == null) {
            synchronized (OkHttpUtils.class) {
                if (okHttpUtils == null) {
                    okHttpUtils = new OkHttpUtils();
                }
            }
        }
        return okHttpUtils;
    }
    //get封装
    public void getData(String url, HashMap<String, String> params, final RequestCollBack requestCallback) {
        StringBuilder urlsb = new StringBuilder();
        String allUrl = "";
        for (Map.Entry<String, String> stringStringEntry : params.entrySet()) {
            urlsb.append("?").append(stringStringEntry.getKey()).append("=").append(stringStringEntry.getValue()).append("&");
        }
        allUrl = url + urlsb.toString().substring(0, urlsb.length() - 1);
        System.out.println("url:" + allUrl);
        final Request request = new Request.Builder()
                .url(allUrl).get().build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            //请求失败
            @Override
            public void onFailure(Call call, IOException e) {
                if (requestCallback != null) {
                    requestCallback.onFailure(call, e);
                }
            }
            //请求成功
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (requestCallback != null) {
                    requestCallback.onResponse(call, response);
                }
            }
        });
    }
    //post请求方式
    public void postData(String url, HashMap<String, String> prams, final RequestCollBack requestCallback) {
        FormBody.Builder formBodyBuilder = new FormBody.Builder();
        if (prams != null && prams.size() > 0) {
            for (Map.Entry<String, String> stringStringEntry : prams.entrySet()) {
                formBodyBuilder.add(stringStringEntry.getKey(), stringStringEntry.getValue());
            }
        }
        Request request = new Request.Builder()
                .url(url).post(formBodyBuilder.build()).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (requestCallback != null) {
                    requestCallback.onFailure(call, e);
                }
            }
            //请求成功
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (requestCallback != null) {
                    requestCallback.onResponse(call, response);
                }
            }
        });
    }
    //封装上传文件
    public void uploadFile(String url, HashMap<String, Object> params, final RequestCollBack requestCallback) {
        //多表单上传的的请求体对象,multipart/form-data
        MultipartBody.Builder builder1 = new MultipartBody.Builder();
        //设置上传类型是表单形式
        builder1.setType(MultipartBody.FORM);
        //1。字符串key,value,2。文件key,value
        for (Map.Entry<String, Object> stringObjectEntry : params.entrySet()) {
            String key = stringObjectEntry.getKey();
            Object value = stringObjectEntry.getValue();
            //如果value类型是文件类型
            if (value instanceof File){
                File file = (File) value;
                //创建文件请求体
                RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
                builder1.addFormDataPart(key,file.getName(),requestBody);
            }else {
                builder1.addFormDataPart(key, (String) value);
            }
        }
        Request request1 = new Request.Builder().post(builder1.build()).url(url).build();
        okHttpClient.newCall(request1).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (requestCallback != null) {
                    requestCallback.onFailure(call, e);
                }
            }
            //请求成功
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (requestCallback != null) {
                    requestCallback.onResponse(call, response);
                }
            }
        });

    }
}

RequestCollBack

public interface RequestCollBack {
    void onFailure(Call call, IOException e);
    void onResponse(Call call, Response response) throws IOException;
}

App

public class App extends Application{

    public final static String BASE_URL = "http://www.zhaoapi.cn/";
    public final static String LOGIN_URL = BASE_URL+"user/login";
    public final static String REG_URL = BASE_URL+"user/reg";
    public final static String PRODUCT_URL = BASE_URL+"product/searchProducts";
    public final static String UPLOAD_URL = BASE_URL+"file/upload";
    @Override
    public void onCreate() {
        super.onCreate();
        //初始化有盟
        initUm();
    }
    private void initUm() {
        //umeng配置
        UMConfigure.init(this,"5a12384aa40fa3551f0001d1"
                ,"umeng",UMConfigure.DEVICE_TYPE_PHONE,"");//58edcfeb310c93091c000be2 5965ee00734be40b580001a0
        //qq登录配置
        PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
        //是否可能打印堆栈信息
        UMConfigure.setLogEnabled(true);
    }
}

DiTuActivity

public class DiTuActivity extends AppCompatActivity {
    private MapView mapView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_di_tu);
        mapView = findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);
    }
    //销毁
    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
    //资源回收,关闭地图时暂停
    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }
    //打开地图时,重新显示当前位置
    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }
    //保存当前地图状态
    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
}

GerengActivity

public class GerengActivity extends AppCompatActivity {
    private ImageView img;
    private String path = Environment.getExternalStorageDirectory()
            + "/pics.jpg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gereng);
        img = findViewById(R.id.img);
    }
    public void xiangce(View view) {
        // 打开相册
        Intent intent = new Intent(Intent.ACTION_PICK);
        // 设置图片的类型
        intent.setType("image/*");
        // 打开相册页面、
        startActivityForResult(intent, 999);
    }
    public void paizhao(View view) {
        // 打开系统拍照页面
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // 把拍完的照片,,存放到sd卡中
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
        // 跳转
        startActivityForResult(intent, 999);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 999 && resultCode == RESULT_OK) {
            //相册对应的回显数据
            Uri uri = data.getData();
            //裁剪
            crop(uri);
        }else if (requestCode == 1000 && resultCode == RESULT_OK) {
            //取出数据
            Bitmap bitmap = data.getParcelableExtra("data");
            img.setImageBitmap(bitmap);
        }
    }
    private void crop(Uri uri) {
        //通过隐式跳转,,,打开裁剪页面
        Intent intent = new Intent("com.android.camera.action.CROP");
        //通过意图对象进行设置
        //设置裁剪图片的类型
        intent.setDataAndType(uri, "image/*");
        //是否允许裁剪
        intent.putExtra("crop", true);
        //裁剪框的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        //输出图片的大小
        intent.putExtra("outputX", 300);
        intent.putExtra("outputY", 300);
        //取消人脸识别
        intent.putExtra("noFaceDetection", false);
        //允许返回数据
        intent.putExtra("return-data", true);
        //回显数据
        startActivityForResult(intent, 1000);
    }
    public void tolistgo(View view) {
        Intent intent = new Intent(GerengActivity.this,ToListGoActivity.class);
        startActivity(intent);
    }
}

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView img_0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img_0 = findViewById(R.id.img_0);
        img_0.setOnClickListener(this);
    }
    //点击小红球,跳转到如图2界面
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_0:
                Intent intent = new Intent(MainActivity.this,QQLoignActivity.class);
                startActivity(intent);
                break;
        }
    }
    //渐变
    public void btn_1(View view) {
        ObjectAnimator btn_1 = ObjectAnimator.ofFloat(img_0,"alpha",0f,1f,0.5f,1f,0f,1f);
        btn_1.setDuration(5000);
        btn_1.start();
    }
    //旋转
    public void btn_2(View view) {
        ObjectAnimator btn_2 = ObjectAnimator.ofFloat(img_0,"rotation",0f,180f,360f,236f,0f);
        btn_2.setDuration(5000);
        btn_2.start();
        btn_2.setRepeatCount(-1);
        //从开始位置循环还是从结束位置循环
        btn_2.setRepeatMode(ValueAnimator.REVERSE);
        btn_2.setRepeatMode(ValueAnimator.RESTART);
        btn_2.cancel();
        btn_2.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                //动画开始的监听
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                //结束的时候
            }

            @Override
            public void onAnimationCancel(Animator animator) {
                //取消的时候
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
                //循环执行
            }
        });
    }
    //平移
    public void btn_3(View view) {
        ObjectAnimator btn_3 = ObjectAnimator.ofFloat(img_0,"translationX",0f,760f);
        btn_3.setDuration(2000);
        btn_3.start();
    }
    //缩放
    public void btn_4(View view) {
        ObjectAnimator btn_4x = ObjectAnimator.ofFloat(img_0,"scaleX",1f,2f);
        btn_4x.setDuration(2000);
        btn_4x.start();
        ObjectAnimator btn_4y = ObjectAnimator.ofFloat(img_0,"scaleY",1f,2f);
        btn_4y.setDuration(2000);
        btn_4y.start();
    }
    //VALUEANMATOR平移
    public void btn_5(View view) {
        ObjectAnimator btn_5a = ObjectAnimator.ofFloat(img_0,"translationX",0f,750f);
        btn_5a.setDuration(5000);
        btn_5a.start();
        ObjectAnimator btn_5c = ObjectAnimator.ofFloat(img_0,"translationY",0f,850f);
        btn_5c.setDuration(5000);
        btn_5c.start();
        ObjectAnimator btn_5b = ObjectAnimator.ofFloat(img_0,"alpha",0f,1f,0.5f,1f,0f,1f);
        btn_5b.setDuration(5000);
        btn_5b.start();
    }
}

QQLoignActivity

public class QQLoignActivity extends AppCompatActivity {

    private UMShareAPI umShareAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qqloign);
        umShareAPI = UMShareAPI.get(this);
    }
    //实现QQ登录
    public void clik_QQview(View view) {
        UMAuthListener authListener = new UMAuthListener() {
            //开始的回调
            @Override
            public void onStart(SHARE_MEDIA platform) {
            }
            //成功的回调
            @Override
            public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
                Toast.makeText(QQLoignActivity.this, "成功了", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(QQLoignActivity.this,GerengActivity.class);
                startActivity(intent);
            }
            //失败的回调
            @Override
            public void onError(SHARE_MEDIA platform, int action, Throwable t) {
                Toast.makeText(QQLoignActivity.this, "失败:" + t.getMessage(),                                     Toast.LENGTH_LONG).show();

            }
            //取消的回调
            @Override
            public void onCancel(SHARE_MEDIA platform, int action) {
                Toast.makeText(QQLoignActivity.this, "取消了", Toast.LENGTH_LONG).show();
            }
        };
        umShareAPI.getPlatformInfo(this, SHARE_MEDIA.QQ, authListener);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}

ToListGoActivity

public class ToListGoActivity extends AppCompatActivity {

    private XRecyclerView Xrv;
    private int pscid = 1;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_list_go);
        initView();
        initData();
    }

    private void initData() {
        HashMap<String, String> prams = new HashMap<>();
        prams.put("pscid", pscid + "");
        OkHttpUtils.getInstance().postData("https://www.zhaoapi.cn/product/getProducts", prams, new RequestCollBack() {
            @Override
            public void onFailure(Call call, IOException e) {
                Toast.makeText(ToListGoActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (200 == response.code()) {
                    String s = response.body().string();
                    //Log.i("aaa",s);
                    if (!TextUtils.isEmpty(s)) {
                        preajson(s);
                    }
                }
            }
        });

    }

    private void preajson(String s) {
        Gson gson = new Gson();
        final ShowBean showBean = gson.fromJson(s, ShowBean.class);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (showBean.data == null) {
                    Toast.makeText(ToListGoActivity.this, "已经没有数据了", Toast.LENGTH_SHORT).show();
                } else {
                    adapter = new MyAdapter(ToListGoActivity.this, showBean);
                    Xrv.setAdapter(adapter);
                }
                adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        startActivity(new Intent(ToListGoActivity.this, DiTuActivity.class));
                    }
                });
            }
        });
    }
    private void initView() {
        Xrv = (XRecyclerView) findViewById(R.id.Xrv);
        Xrv.setLayoutManager(new LinearLayoutManager(ToListGoActivity.this));
        Xrv.setLoadingMoreEnabled(true);
        Xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                pscid = 1;
                initData();
                Xrv.refreshComplete();
            }
            @Override
            public void onLoadMore() {
                pscid += 1;
                initData();
                Xrv.loadMoreComplete();
            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/QQ849410011/article/details/81837719
今日推荐