Java-Android Stuidio(续)
Posted 怪歌
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java-Android Stuidio(续)相关的知识,希望对你有一定的参考价值。
Java-android
PhaseTwo(Android)
okgo
okhttp的封装 简易版Okhttp
这里继承了上一个项目中的BaseActivity,如果不明白的点下面的链接,并打开PhaseTwo(Android)
上一期的Java-Android.
package com.bawei.zhuangao2.day3;
import android.Manifest;
import android.annotation.SuppressLint;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
import com.bawei.zhuangao2.R;
import com.bawei.zhuangao2.day1.HeadInterceptor;
import com.bawei.zhuangao2.day2.base.BaseActivity;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
//一样继承于BaseActivity
public class OkGoActivity extends BaseActivity {
//用于下载图片展示
private ImageView coverIv;
/**
* 绑定主要视图
* @return
*/
@Override
public int bandLayout() {
return R.layout.activity_okhttp;
}
/**
* 初始化控件 并设置点击事件
*/
@Override
public void initView() {
//获取隐私权限(这里隐私权限指的是:手机联系人,读写SD卡,手机信息等等)
//这里获取了写入SD卡的权限
PermissionUtils.permission(Manifest.permission.WRITE_EXTERNAL_STORAGE).request();
findViewById(R.id.btn1).setOnClickListener(this);
findViewById(R.id.btn2).setOnClickListener(this);
findViewById(R.id.btn3).setOnClickListener(this);
findViewById(R.id.btn4).setOnClickListener(this);
findViewById(R.id.btn5).setOnClickListener(this);
findViewById(R.id.btn6).setOnClickListener(this);
coverIv = findViewById(R.id.image);
}
@Override
public void initData() {
}
/**
* 点击事件
* @param v
*/
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
//Get方式获取
OkGo.<String>get("https://gank.io/api/v2/banners").execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
//对Response 做了解析
//这里还是异步线程
LogUtils.d(response.body());
}
@Override
public void onCacheSuccess(Response<String> response) {
super.onCacheSuccess(response);
LogUtils.d("onCacheSuccess" + response.body());
}
});
break;
case R.id.btn2:
//Post Json 通过upJson("")提交代码
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("username", "1234567");
jsonObject.put("pwd", "123456");
jsonObject.put("sex", "1");
jsonObject.put("birthday", "2000-9-9");
} catch (JSONException e) {
e.printStackTrace();
}
OkGo.<String>post("http://39.98.153.96:8082/api/WeekTest/register")
.upJson(jsonObject.toString()).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
//Response 对做了解析
//这里还是异步线程
LogUtils.d(response.body());
}
});
break;
case R.id.btn3:
//Post From
//username=1903001&pwd=123456
OkGo.<String>post("http://39.98.153.96:8082/api/WeekTest/login?username=1903001&pwd=123456")
.params("username", "1903001")
.params("pwd", "123456")
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
//Response 对做了解析
//这里还是异步线程
LogUtils.d(response.body());
}
});
break;
case R.id.btn4:
//下载
OkGo.<File>get("https://pic.downk.cc/item/5e7b64fd504f4bcb040fae8f.jpg").execute(new FileCallback() {
@Override
public void onSuccess(final Response<File> response) {
runOnUiThread(new Runnable() {
@Override
public void run() {
LogUtils.d(response.body().getPath());
coverIv.setImageBitmap(BitmapFactory.decodeFile(response.body().getPath()));
}
});
}
});
break;
case R.id.btn5:
break;
case R.id.btn6:
//文件上传
File file = new File("/storage/emulated/0/download/1382400000b859e1c2339");
coverIv.setImageBitmap(BitmapFactory.decodeFile(file.getPath()));
OkGo.<String>post("http://117.51.155.210/hfs/")
//发送格式为file
.params("file",file)
//分段发送
.isMultipart(true)
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
LogUtils.d(response.body());
}
});
break;
}
}
}
Glide工具
认识Glide
Glide是一个图片加载框架,功能强大,一般用于一些视图显示图片,从url中加载图片等等,也可以进行裁剪图片,达到视觉美观
Glide方法类
/**
* 单例 双重校验 双重锁
*/
public class ImageManger {
public void showImage(String url, ImageView view){
Glide.with(Utils.getApp()).load(url)
//关闭缓存
.skipMemoryCache(true)
//关闭磁盘缓存
.diskCacheStrategy(DiskCacheStrategy.NONE)
//加载过程显示
.placeholder(R.mipmap.ic_launcher)
//加载失败显示
.error(R.mipmap.ic_launcher)
.into(view);
}
public void showR(String url, ImageView view,int radius){
Glide.with(Utils.getApp()).load(url)
.transform(new RoundedCorners(radius))
//加载过程显示
.placeholder(R.mipmap.ic_launcher)
//加载失败显示
.error(R.mipmap.ic_launcher)
.into(view);
}
public void showC(String url, ImageView view){
Glide.with(Utils.getApp()).load(url)
//圆形
.circleCrop()
.into(view);
}
//多线程可见
private static volatile ImageManger imageManger;
//饿汉式单例
public static ImageManger getInstance(){
if (imageManger==null){
//这里上锁
synchronized (ImageManger.class){
if (imageManger==null){
imageManger=new ImageManger();
}
}
}
return imageManger;
}
}
Activity类
public class GlideActivity extends BaseActivity {
private android.widget.ImageView actGlideIv01;
private android.widget.ImageView actGlideIv02;
private android.widget.ImageView actGlideIv03;
//初始化主视图
@Override
public int bandLayout() {
return R.layout.activity_glide;
}
//初始化控件
@Override
public void initView() {
actGlideIv01 = findViewById(R.id.act_glide_iv_01);
actGlideIv02 = findViewById(R.id.act_glide_iv_02);
actGlideIv03 = findViewById(R.id.act_glide_iv_03);
}
//加载数据(显示图片)
@Override
public void initData() {
//实例化调用方法 完成Glide加载
ImageManger imageManger = new ImageManger();
imageManger.showImage("https://pic.downk.cc/item/5e7b64fd504f4bcb040fae8f.jpg",actGlideIv01);
imageManger.showC("https://pic.downk.cc/item/5e7b64fd504f4bcb040fae8f.jpg",actGlideIv02);
imageManger.showR("https://pic.downk.cc/item/5e7b64fd504f4bcb040fae8f.jpg",actGlideIv03,50);
}
}
以上是关于Java-Android Stuidio(续)的主要内容,如果未能解决你的问题,请参考以下文章