无需解压获取zip中的图片资源 实现3D旋转

编程入门 行业动态 更新时间:2024-10-20 15:57:11

无需解压获取zip中的图片<a href=https://www.elefans.com/category/jswz/34/1770980.html style=资源 实现3D旋转"/>

无需解压获取zip中的图片资源 实现3D旋转

**

冲刺在搬砖的路上

**
知识点:

  1. 对zipFile类有一定程度的理解;
    首先实例化ZipFile类
    ZipFile zipFile = new ZipFile(zipPath);
    参数zipPath是压缩包的文件路径

  2. 获取zip文件中的目录及文件列表
    Enumeration e = (Enumeration) zipFile.entries();

例子

 try {zipFile = new ZipFile(zipPath);// 获取zip文件中的目录及文件列表Enumeration<ZipEntry> e = (Enumeration<ZipEntry>) zipFile.entries();ZipEntry entry = null;pics = new ArrayList<>();while (e.hasMoreElements()) {entry = e.nextElement();if (!entry.isDirectory()) {// 如果文件不是目录,则添加到列表中pics.add(entry);}}initData(0);startTime();} catch (IOException e) {e.printStackTrace();}

将所有的文件实体存放在pics集合中

如何使用pics数据源呢,我做了一个3D旋转的列子,废话不多说上代码片段:

 
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;import com.jbh.deringmobile.R;
import com.jbh.deringmobile.Voice.U;
import com.jbh.deringmobile.application.MyApplication;
import com.jbh.deringmobile.utils.Constant;
import com.jbh.deringmobile.utils.Util;
import com.jbh.deringmobile.weights.Touch3DImageView;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;public class DialogShow3DImage extends Dialog implements DialogInterface.OnDismissListener ,Touch3DImageView.OnTouchStatusListener {private static final long delayMillis = 50;private String zipPath ;private Touch3DImageView show_image;private ArrayList<ZipEntry> pics;private ZipFile zipFile= null;private boolean isTouch=false;private int index;private ImageView img_zhiYin ;private Handler handler=new Handler();private LinearLayout lay_zy;private ObjectAnimator anim;public DialogShow3DImage(@NonNull Context context) {super(context, R.style.dialog_0);View viewDialog = LayoutInflater.from(MyApplication.context).inflate(R.layout.layout_3d, null);setContentView(viewDialog,getlayoutParams());setCanceledOnTouchOutside(true);setOnDismissListener(this);initView(viewDialog);viewDialog.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dismiss();}});}/**** @param b*/public void save(boolean b){SharedPreferences sharedPreferences=getContext().getSharedPreferences(Constant.DATA,0);SharedPreferences.Editor editor = sharedPreferences.edit();editor.putBoolean("first",b);editormit();}/**** @return*/public Boolean read(){SharedPreferences sharedPreferences=getContext().getSharedPreferences(Constant.DATA,0);boolean isFirst = sharedPreferences.getBoolean("first", false);return isFirst;}/**** @return*/public ViewGroup.LayoutParams getlayoutParams(){Display display = getWindow().getWindowManager().getDefaultDisplay();int width = display.getWidth();int height = display.getHeight();//设置dialog的宽高为屏幕的宽高ViewGroup.LayoutParams layoutParams = new  ViewGroup.LayoutParams(width, height- Util.getStatusBarHeight());return layoutParams;}/*** 初始化** @param viewDialog*/private void initView(View viewDialog) {show_image=viewDialog.findViewById(R.id.show_image);show_image.setOnTouchStatusListener(this);img_zhiYin = viewDialog.findViewById(R.id.img_zhiYin);lay_zy=viewDialog.findViewById(R.id.lay_zy);ViewGroup.LayoutParams params = lay_zy.getLayoutParams();int w=Util.getWidth();params.width=w;params.height=Util.getHeight();lay_zy.setLayoutParams(params);anim = ObjectAnimator.ofFloat(img_zhiYin, "translationX", 100, -80);anim .setDuration(1000);anim .setRepeatCount(0);anim.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {super.onAnimationEnd(animation);ObjectAnimator a=ObjectAnimator.ofFloat(lay_zy,"alpha",1,0);a.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {super.onAnimationEnd(animation);lay_zy.setVisibility(View.GONE);}});a.start();}});anim .start();//        if (read()){
//            lay_zy.setVisibility(View.GONE);
//        }else {
//            lay_zy.setVisibility(View.VISIBLE);
//            save(true);
//        }lay_zy.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {lay_zy.setVisibility(View.GONE);anim.cancel();}});}/*** 获取zip路径** @param zipPath*/public void setZipPath(String zipPath){this.zipPath = zipPath;}/*** 初始化*/public void init3D(){try {zipFile = new ZipFile(zipPath);// 获取zip文件中的目录及文件列表Enumeration<ZipEntry> e = (Enumeration<ZipEntry>) zipFile.entries();ZipEntry entry = null;pics = new ArrayList<>();while (e.hasMoreElements()) {entry = e.nextElement();if (!entry.isDirectory()) {// 如果文件不是目录,则添加到列表中pics.add(entry);}}initData(0);startTime();} catch (IOException e) {e.printStackTrace();}}private Runnable runnable=new Runnable() {@Overridepublic void run() {startTime();if (!isTouch){ //开关控制触摸的时候是否旋转index++;if (index<pics.size()-1){initData(index);}else {index=0;}}}};/*** 开始计时*/public void startTime(){handler.postDelayed(runnable,delayMillis);}/**** @param position*/public void initData(int position){try {BitmapFactory.Options opts = new BitmapFactory.Options();Bitmap  bitmap = BitmapFactory.decodeStream(zipFile.getInputStream(pics.get(position)), null, opts);show_image.setImageBitmap(bitmap);} catch (IndexOutOfBoundsException e){e.printStackTrace();}catch (IOException e) {e.printStackTrace();}}@Overridepublic void onDismiss(DialogInterface dialog) {handler.removeCallbacks(runnable);}@Overridepublic void down() {isTouch = true ;}@Overridepublic void up() {isTouch = false ;}@Overridepublic void onScrollIndex(float scrollX) {if (pics!=null&&pics.size()>0){int position= (int) ((pics.size()-1)*scrollX);Log.v("position","position="+position);if (position>0){index =  pics.size() - position-1;}else {index = - position;}initData(index);}}
}

Touch3DImageView.java 类


@SuppressLint("AppCompatCustomView")
public class Touch3DImageView extends ImageView implements View.OnTouchListener {private float lastX;private int maxMove = 700;private int touchSlop ;public Touch3DImageView(Context context) {this(context,null);}public Touch3DImageView(Context context, @Nullable AttributeSet attrs) {this(context, attrs,0);}public Touch3DImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);setOnTouchListener(this);touchSlop =  ViewConfiguration.get(context).getScaledTouchSlop();}@Overridepublic boolean onTouch(View v, MotionEvent event) {float x = event.getRawX();switch (event.getAction()){case MotionEvent.ACTION_DOWN:onTouchStatusListener.down();lastX = x ;break;case MotionEvent.ACTION_MOVE:float move = x -lastX;if (Math.abs(move)>touchSlop){if (Math.abs(move) >= maxMove){if (move>0){move=maxMove;}else {move=-maxMove;}}if (move>0){move = move - touchSlop;}else {move = move + touchSlop;}float scale= (float) (move*1.0/maxMove*1.0);onTouchStatusListener.onScrollIndex(scale);}break;case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:onTouchStatusListener.up();break;}return true;}public interface OnTouchStatusListener{void down();void up();void onScrollIndex(float scrollX);}private OnTouchStatusListener onTouchStatusListener;public void setOnTouchStatusListener(OnTouchStatusListener onTouchStatusListener) {this.onTouchStatusListener = onTouchStatusListener;}
}

3D旋转实现原理其实就是一个定时器通过间断时间不断循环切换显示图片的过程,其实这个和帧动画是一个原理,文章到这儿已经结束了 ,

更多推荐

无需解压获取zip中的图片资源 实现3D旋转

本文发布于:2024-03-23 19:36:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1742000.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:资源   图片   zip

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!