Android實(shí)現(xiàn)圖片點(diǎn)擊放大
本文實(shí)例為大家分享了Android實(shí)現(xiàn)圖片點(diǎn)擊放大的具體代碼,供大家參考,具體內(nèi)容如下
在我的項(xiàng)目中,有點(diǎn)擊圖片banner后放大瀏覽的功能。我的做法就是創(chuàng)建一個(gè)專(zhuān)門(mén)的圖片顯示Activity,布局里面用ViewPage,這樣就能控制圖片的左右滑動(dòng),并且控制首先顯示第幾張圖片。
功能是ok的,顯示也是正常的。但我花費(fèi)了好幾天的時(shí)間來(lái)實(shí)現(xiàn)、完善這個(gè)功能。
ShowMoreImageActivity
/**
* 圖片放大
*/
public class ShowMoreImageActivity extends BaseActivity {
@FindId(R.id.vp)
private ViewPager vp;
@FindId(R.id.ll_point)
private LinearLayout ll_point;
private List<String> imgs;
@FindId(R.id.btn_save)
private ImageView btn_save;
private int index;
public static int type;
private Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_more_image);
initViews();
activity = this;
}
private void initViews() {
AutoFindId.findId(context);
imgs = (ArrayList<String>) getIntent().getSerializableExtra("img");
index = getIntent().getIntExtra("index", 0);
type = getIntent().getIntExtra("type", 0);
vp.setAdapter(new MoreImgPagerAdapter(context, imgs));
vp.addOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
index = arg0;
setUpPoint(imgs.size(), arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
setUpPoint(imgs.size(), 0);
vp.setCurrentItem(index);
}
protected void downLoad(final String urls) {
String[] split = urls.split("\\?");
final String url = split[0];
if (url.startsWith("file")) {
G.toast(context, "此為本地圖片,不用下載,路徑為" + url.replace("file://", ""));
return;
}
if (OKHttpUtils.isNetworkAvailable(context)) {
G.showPd(context);
TDUtils.execute(new Runnable() {
@Override
public void run() {
try {
File file = new File(C.getDownloadPath());
if (!file.exists()) {
file.mkdir();
}
File jpg = new File(C.getDownloadPath() + G.urlToFileName(url));
// 如果已經(jīng)存在則不需要下載
if (jpg != null && jpg.exists()) {
G.dismissProgressDialogInThread();
G.toastInThread(context,
"該文件已被下載到" + jpg.getParent() + context.getResources().getString(R.string.xia));
return;
}
// 先從緩存中查找
File tmpFile = NetAide.getBitmapUtils().getBitmapFileFromDiskCache(url);
if (tmpFile != null && tmpFile.exists()) {
G.look("---從緩存中查找到圖片----");
Bitmap bm = BitmapFactory.decodeFile(tmpFile.getAbsolutePath());
FileOutputStream fos = new FileOutputStream(jpg);
bm.compress(CompressFormat.JPEG, 100, fos);
fos.close();
G.dismissProgressDialogInThread();
// 通知圖庫(kù)更新
C.noticeImageRefresh(context, jpg);
G.toastInThread(context, context.getResources().getString(R.string.downLoadUrl)
+ jpg.getParent() + context.getResources().getString(R.string.xia));
return;
}
// 從網(wǎng)絡(luò)上下載保存
Bitmap bm = BitmapFactory.decodeStream(new URL(url).openStream());
FileOutputStream fos = new FileOutputStream(jpg);
bm.compress(CompressFormat.JPEG, 100, fos);
fos.close();
G.dismissProgressDialogInThread();
// 通知圖庫(kù)更新
C.noticeImageRefresh(context, jpg);
G.toastInThread(context, "你現(xiàn)在可以在圖庫(kù)中查看該圖片了");
} catch (Exception e) {
e.printStackTrace();
G.dismissProgressDialogInThread();
G.toastInThread(context, context.getResources().getString(R.string.downLoadFail));
File jpg = new File(C.getDownloadPath() + G.urlToFileName(url));
if (jpg != null && jpg.exists()) {
jpg.delete();
}
}
}
});
}
}
private void setUpPoint(int size, int choose) {
ll_point.removeAllViews();
if (size <= 1) {
return;
}
for (int i = 0; i < size; i++) {
ImageView point = new ImageView(context);
point.setLayoutParams(new LinearLayout.LayoutParams(DensityUtil.dip2px(context, 15), -2));
point.setScaleType(ScaleType.FIT_CENTER);
if (i == choose) {
point.setImageResource(R.drawable.white_choosed);
} else {
point.setImageResource(R.drawable.white_no_choosed);
}
ll_point.addView(point);
}
}
public void doClcik(View view) {
switch (view.getId()){
case R.id.btn_save:
PermissionUtils permissionUtils = new PermissionUtils();
permissionUtils.setPermission(this, "存儲(chǔ)", "保存圖片", new PermissionUtils.AfterPermission() {
@Override
public void doNext() {
downLoad(imgs.get(index));
}
},Manifest.permission.WRITE_EXTERNAL_STORAGE);
break;
}
}
}
對(duì)應(yīng)布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical">
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="25dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="match_parent"
android:onClick="onFinish">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:background="@drawable/nav_back" />
</LinearLayout>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<ImageView
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:onClick="doClcik"
android:src="@drawable/download_img" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_point"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="40dp"
android:gravity="center"
android:orientation="horizontal"/>
</FrameLayout>
MoreImgPagerAdapter
public class MoreImgPagerAdapter extends PagerAdapter {
private Context context;
private List<String> images;
private SparseArray<SoftReference<View>> ivs;
public MoreImgPagerAdapter(Context context, List<String> images) {
this.context = context;
this.images = images;
ivs = new SparseArray<SoftReference<View>>();
}
@Override
public int getCount() {
return images.size();
}
@Override
public void destroyItem(ViewGroup arg0, int arg1, Object arg2) {
SoftReference<View> reference = ivs.get(arg1);
if (reference != null && reference.get() != null) {
arg0.removeView(reference.get());
}
}
@Override
public Object instantiateItem(ViewGroup arg0, final int arg1) {
SoftReference<View> reference = ivs.get(arg1);
if (reference == null || reference.get() == null) {
View v = LayoutInflater.from(context).inflate(R.layout.item_show_more_image, null);
reference = new SoftReference<View>(v);
ivs.put(arg1, reference);
}
View v = reference.get();
final ViewHolder holder = new ViewHolder(v);
Glide.with(context).asBitmap().load(images.get(arg1)).into(holder.image);
arg0.addView(v);
return v;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0.equals(arg1);
}
class ViewHolder {
@FindId(R.id.image)
private ImageView image;
@FindId(R.id.rl_percent)
private RelativeLayout rl_percent;
@FindId(R.id.tv_percent)
private TextView tv_percent;
@FindId(R.id.iv_top)
private ImageView iv_top;
public ViewHolder(View v) {
AutoFindId.findIdByView(this, v);
}
}
}
對(duì)應(yīng)布局:
<?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="match_parent" android:background="@android:color/black" android:orientation="vertical" > <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="50dp" android:layout_marginTop="70dp" > <ImageView android:layout_gravity="center" android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" /> <ImageView android:id="@+id/iv_top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|right" android:visibility="gone" android:background="@drawable/shuiyin" /> </FrameLayout> <RelativeLayout android:visibility="gone" android:id="@+id/rl_percent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" > <ProgressBar android:layout_width="40dp" android:layout_height="40dp" /> <TextView android:id="@+id/tv_percent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textColor="@android:color/white" android:textSize="12sp" /> </RelativeLayout> </RelativeLayout>
上面都是次要的,因?yàn)槲野l(fā)現(xiàn)了一個(gè)更為簡(jiǎn)便的輪子。
在我的項(xiàng)目中,我只需要兩步就完成了此功能。
第一步:
// 查看大圖 implementation 'com.github.SherlockGougou:BigImageViewPager:v4_6.1.1'
第二步:
在點(diǎn)擊圖片事件那里調(diào)用:
ImagePreview .getInstance() // 上下文,必須是activity,不需要擔(dān)心內(nèi)存泄漏,本框架已經(jīng)處理好; .setContext(context) // 設(shè)置從第幾張開(kāi)始看(索引從0開(kāi)始) .setIndex(position) // 有三種設(shè)置數(shù)據(jù)集合的方式,根據(jù)自己的需求進(jìn)行三選一: // 1:第一步生成的imageInfo List //.setImageInfoList(imageInfoList) // 2:直接傳url List .setImageList(imageList) // 3:只有一張圖片的情況,可以直接傳入這張圖片的url //.setImage(String image) // 開(kāi)啟預(yù)覽 .start();
就這樣完成了圖片放大瀏覽、下載的功能,在這里記錄下。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android點(diǎn)擊事件之多點(diǎn)觸摸與手勢(shì)識(shí)別的實(shí)現(xiàn)
- Android開(kāi)發(fā)返回鍵明暗點(diǎn)擊效果的實(shí)例代碼
- Android實(shí)現(xiàn)自動(dòng)點(diǎn)擊無(wú)障礙服務(wù)功能的實(shí)例代碼
- Android自定義SeekBar實(shí)現(xiàn)滑動(dòng)驗(yàn)證且不可點(diǎn)擊
- android實(shí)現(xiàn)點(diǎn)擊按鈕控制圖片切換
- Android實(shí)現(xiàn)點(diǎn)擊某個(gè)按鈕指定位置彈出布局
- android實(shí)現(xiàn)點(diǎn)擊圖片全屏展示效果
- Android Button按鈕點(diǎn)擊背景和文字變化操作
- Android Studio finish()方法的使用與解決app點(diǎn)擊“返回”(直接退出)
- 關(guān)于android連續(xù)點(diǎn)擊出現(xiàn)多個(gè)Activity界面的解決方法
- Android實(shí)現(xiàn)WebView點(diǎn)擊攔截跳轉(zhuǎn)原生
- Android擴(kuò)大View點(diǎn)擊范圍的方法
相關(guān)文章
Android 應(yīng)用中跳轉(zhuǎn)到應(yīng)用市場(chǎng)評(píng)分示例
本篇文章主要介紹了Android 應(yīng)用中跳轉(zhuǎn)到應(yīng)用市場(chǎng)評(píng)分示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02
android activity設(shè)置無(wú)標(biāo)題實(shí)現(xiàn)全屏
本文將詳細(xì)介紹Android如何設(shè)置Activity全屏和無(wú)標(biāo)題的實(shí)現(xiàn)方法,需要的朋友可以參考下2012-12-12
Android實(shí)現(xiàn)捕獲TextView超鏈接的方法
這篇文章主要介紹了Android實(shí)現(xiàn)捕獲TextView超鏈接的方法,涉及Android查找TextView中超鏈接的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android?Scroller實(shí)現(xiàn)彈性滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android?Scroller實(shí)現(xiàn)彈性滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android-實(shí)現(xiàn)切換Fragment頁(yè)功能的實(shí)現(xiàn)代碼
本篇文章主要介紹了Android-實(shí)現(xiàn)切換Fragment頁(yè)功能的實(shí)現(xiàn)代碼,具有一定的參加價(jià)值,有興趣的可以了解一下。2017-02-02
解決Android studio Error:(30, 31) 錯(cuò)誤: 程序包 不存在的問(wèn)題
這篇文章主要介紹了解決Android studio Error:(30, 31) 錯(cuò)誤: 程序包 不存在的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android實(shí)現(xiàn)蝸牛進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)蝸牛進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android基于ibeacon實(shí)現(xiàn)藍(lán)牙考勤功能
這篇文章主要為大家詳細(xì)介紹了Android基于ibeacon實(shí)現(xiàn)藍(lán)牙考勤功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Android實(shí)現(xiàn)圖片在屏幕內(nèi)縮放和移動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android控制圖片在屏幕內(nèi)縮放和移動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Android編程實(shí)現(xiàn)滑動(dòng)按鈕功能詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)滑動(dòng)按鈕功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android實(shí)現(xiàn)滑動(dòng)按鈕的功能、布局及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-02-02

