之前实现加载一张网络图片并且随意拉大,缩放都比较麻烦,现在使用第三方开源库来实现就很容易。具体实现没什么难度,就直接上代码了。
现在gradle中加入
compile 'com.android.support:design:25.0.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.github.chrisbanes.photoview:library:1.2.4'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
页面布局如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fl_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
<ImageView
android:id="@+id/iv_gril"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:transitionName="shareView" />
</LinearLayout>
</FrameLayout>
图片界面继承BaseActivity
/**
* Created by an on 2017/6/10.
*/
class ImagViewActivity extends BaseActivity{
private Bitmap mBitmap;
private PhotoViewAttacher mAttacher;
private ImageView img;
private Toolbar toolbar;
@Override
public int getLayoutId() {
return R.layout.activity_imgeview;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
toolbar = (Toolbar) findViewById(R.id.toolbar);
img = (ImageView) findViewById(R.id.iv_gril);
setToolbar();
initData();
}
private void setToolbar() {
toolbar.setTitle("选择日期");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private void initData() {
String mImgUrl = getIntent().getStringExtra("url");
ImageLoader.load(this, mImgUrl, new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
mBitmap = resource;
img.setImageBitmap(mBitmap);
//进行缩放查看图片
mAttacher = new PhotoViewAttacher(img);
}
});
}
}
图片加载的封装类
public class ImageLoader {
public static void load(Context context, String url, ImageView imageView) {
if (!TextUtils.isEmpty(url)) {
Glide.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);
}
}
public static void load(Context context, String url, SimpleTarget target) {
if (!TextUtils.isEmpty(url)) {
Glide.with(context)
.load(url)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(target);
}
}
public static void load(Context context, ImageView imageView) {
Glide.with(context)
.load(R.drawable.default_img)
.placeholder(R.drawable.default_img)
.into(imageView);
}
}
BaseActivity封装如下:
/**
* Created by an on 2017/6/10.
*/
public abstract class BaseActivity extends AppCompatActivity {
//定义一个Activity管理器
private List<Activity> mActivitys=new ArrayList<>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
mActivitys.add(this);
ButterKnife.bind(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
mActivitys.remove(this);
}
//退出所有的Activity
public void finishAll(){
for (Activity activity:mActivitys) {
if (activity!=null) {
activity.finish();
}
}
}
//设置标题栏
protected void setToolBar(Toolbar toolbar,String title){
toolbar.setTitle(title);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP){
finishAfterTransition();
}else{
finish();
}
}
});
}
public abstract int getLayoutId();
}
就是这么简单。
网友评论