From 81c4f6a5f3862b8266192909841a10c5a47d7403 Mon Sep 17 00:00:00 2001 From: Asha Date: Mon, 4 Sep 2017 00:24:06 +0800 Subject: [PATCH] recycler view support part 1. --- app/build.gradle | 8 +- app/src/main/AndroidManifest.xml | 14 +- .../RecyclerViewActivity.java | 318 +++++++++++++ app/src/main/res/layout/activity_main.xml | 13 + .../layout/activity_md_using_surface_view.xml | 432 ++++++++---------- .../main/res/layout/feed_panorama_layout.xml | 25 + .../main/res/layout/feed_simple_layout.xml | 21 + vrlib/build.gradle | 6 +- .../java/com/asha/vrlib/MD360Renderer.java | 3 +- .../com/asha/vrlib/plugins/MDAbsLinePipe.java | 7 + .../com/asha/vrlib/plugins/MDAbsPlugin.java | 8 + .../strategy/projection/PlaneProjection.java | 14 +- .../vrlib/texture/MD360BitmapTexture.java | 1 + .../apps/muzei/render/GLTextureView.java | 29 +- 14 files changed, 644 insertions(+), 255 deletions(-) create mode 100644 app/src/main/java/com/asha/md360player4android/RecyclerViewActivity.java create mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/layout/feed_panorama_layout.xml create mode 100644 app/src/main/res/layout/feed_simple_layout.xml diff --git a/app/build.gradle b/app/build.gradle index 97a4bcd6..b0c072ce 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,8 +1,8 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 23 - buildToolsVersion '25.0.0' + compileSdkVersion 25 + buildToolsVersion '25.0.2' defaultConfig { applicationId "com.asha.md360player4android" @@ -23,9 +23,11 @@ android { dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:23.2.0' + compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.squareup.picasso:picasso:2.5.2' //required, enough for most devices. + compile 'com.android.support:recyclerview-v7:25.3.1' + compile 'com.android.support:cardview-v7:25.3.1' compile 'tv.danmaku.ijk.media:ijkplayer-java:0.6.0' compile 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.6.0' compile project(path: ':vrlib') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 59fb70b2..00bcc7e8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,15 +13,21 @@ android:supportsRtl="true" android:theme="@style/AppTheme"> - - - - + + + + + + + + + + diff --git a/app/src/main/java/com/asha/md360player4android/RecyclerViewActivity.java b/app/src/main/java/com/asha/md360player4android/RecyclerViewActivity.java new file mode 100644 index 00000000..b49e9ceb --- /dev/null +++ b/app/src/main/java/com/asha/md360player4android/RecyclerViewActivity.java @@ -0,0 +1,318 @@ +package com.asha.md360player4android; + +import android.app.Activity; +import android.content.ContentResolver; +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import android.os.Bundle; +import android.support.annotation.DrawableRes; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import com.asha.vrlib.MDVRLibrary; +import com.asha.vrlib.texture.MD360BitmapTexture; +import com.google.android.apps.muzei.render.GLTextureView; +import com.squareup.picasso.Picasso; +import com.squareup.picasso.Target; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +import static com.asha.vrlib.MDVRLibrary.INTERACTIVE_MODE_CARDBORAD_MOTION_WITH_TOUCH; + +public class RecyclerViewActivity extends AppCompatActivity { + + private Uri[] sMockData; + + private static final String TAG = "MainActivity"; + + private VRLibManager manager; + + public RecyclerViewActivity() { + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + sMockData = new Uri[] { + getDrawableUri(R.drawable.bitmap360) + ,getDrawableUri(R.drawable.texture) + }; + + setContentView(R.layout.activity_main); + manager = new VRLibManager(this); + + RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); + final LinearLayoutManager layoutManager = new LinearLayoutManager(this); + final FeedAdapter adapter = new FeedAdapter(); + recyclerView.setLayoutManager(layoutManager); + recyclerView.setItemAnimator(null); + recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrollStateChanged(RecyclerView recyclerView, int newState) { + if (newState != RecyclerView.SCROLL_STATE_DRAGGING) { + int pos = layoutManager.findFirstCompletelyVisibleItemPosition(); + adapter.onPosition(pos); + } + } + }); + recyclerView.setAdapter(adapter); + } + + @Override + protected void onResume() { + super.onResume(); + manager.fireResumed(); + } + + @Override + protected void onPause() { + super.onPause(); + manager.firePaused(); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + manager.fireDestroy(); + } + + private static class FeedModel { + + private final Uri uri; + private final int index; + private int pos; + + public FeedModel(int index, Uri uri) { + this.index = index; + this.uri = uri; + } + + public void setPos(int pos) { + this.pos = pos; + } + } + + private abstract class FeedVH extends RecyclerView.ViewHolder { + + public FeedVH(ViewGroup vp, int layoutId) { + super(create(vp, layoutId)); + } + + public abstract void bind(FeedModel feedModel); + } + + private class FeedSimpleVH extends FeedVH { + + public FeedSimpleVH(ViewGroup vp) { + super(vp, R.layout.feed_simple_layout); + } + + @Override + public void bind(FeedModel feedModel) { + } + } + + private class FeedVRVH extends FeedVH implements MDVRLibrary.IBitmapProvider { + + private ImageView cover; + + private TextView text; + + private GLTextureView glTextureView; + + private ViewGroup parent; + + private MDVRLibrary vrlib; + + private FeedModel model; + + public FeedVRVH(ViewGroup vp) { + super(vp, R.layout.feed_panorama_layout); + cover = (ImageView) itemView.findViewById(R.id.feed_img_cover); + text = (TextView) itemView.findViewById(R.id.feed_text); + glTextureView = (GLTextureView) itemView.findViewById(R.id.feed_texture_view); + parent = (ViewGroup) glTextureView.getParent(); + } + + @Override + public void bind(FeedModel model) { + Log.d(TAG, "FeedVRVH bind."); + this.model = model; + // Picasso.with(itemView.getContext()).load(model.url).into(cover); + ensureVRLib(); + vrlib.notifyPlayerChanged(); + /* + if (model.pos == model.index) { + text.setText("on"); + if (glTextureView.getParent() == null) { + // parent.addView(glTextureView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); + } + + } else { + text.setText("off"); + // parent.removeView(glTextureView); + } + */ + } + + private void ensureVRLib() { + if (vrlib == null) { + vrlib = manager.create(this, glTextureView); + } + } + + @Override + public void onProvideBitmap(final MD360BitmapTexture.Callback callback) { + if (model == null) { + return; + } + Log.d(TAG, "onProvideBitmap"); + Picasso.with(itemView.getContext()).load(model.uri).into(new Target() { + @Override + public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { + Log.d(TAG, "onProvideBitmap onBitmapLoaded"); + + vrlib.onTextureResize(bitmap.getWidth(), bitmap.getHeight()); + callback.texture(bitmap); + } + + @Override + public void onBitmapFailed(Drawable errorDrawable) { + + } + + @Override + public void onPrepareLoad(Drawable placeHolderDrawable) { + + } + }); + } + } + + private Uri getDrawableUri(@DrawableRes int resId){ + Resources resources = getResources(); + return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + resources.getResourcePackageName(resId) + '/' + resources.getResourceTypeName(resId) + '/' + resources.getResourceEntryName(resId) ); + } + + private class FeedAdapter extends RecyclerView.Adapter { + + private List feeds = new ArrayList<>(); + + public FeedAdapter() { + int i = -1; + while (i++ < 50) { + Uri url = sMockData[(int) (Math.random() * sMockData.length)]; + feeds.add(new FeedModel(i, url)); + } + } + + @Override + public FeedVH onCreateViewHolder(ViewGroup parent, int viewType) { + if (viewType == 0) { + return new FeedVRVH(parent); + } else { + return new FeedSimpleVH(parent); + } + + } + + @Override + public void onBindViewHolder(FeedVH holder, int position) { + holder.bind(feeds.get(position)); + } + + @Override + public void onViewRecycled(FeedVH holder) { + super.onViewRecycled(holder); + } + + @Override + public int getItemViewType(int position) { + return position == 0 ? 0 : 1; + } + + @Override + public int getItemCount() { + return feeds.size(); + } + + private int prevPos = -1; + public void onPosition(int pos) { + if (prevPos == pos) { + return; + } + + for (FeedModel feedModel : feeds) { + feedModel.setPos(pos); + } + + notifyItemChanged(prevPos); + notifyItemChanged(pos); + prevPos = pos; + } + } + + private static View create(ViewGroup vp, int layout) { + return LayoutInflater.from(vp.getContext()).inflate(layout, vp, false); + } + + private static class VRLibManager { + private Activity activity; + + private boolean isResumed; + + private List libs = new LinkedList<>(); + + public VRLibManager(Activity activity) { + this.activity = activity; + } + + public MDVRLibrary create(MDVRLibrary.IBitmapProvider provider, GLTextureView textureView) { + MDVRLibrary lib = MDVRLibrary.with(activity). + asBitmap(provider). + interactiveMode(INTERACTIVE_MODE_CARDBORAD_MOTION_WITH_TOUCH). + build(textureView); + add(lib); + return lib; + } + + private void add(MDVRLibrary lib) { + if (isResumed) { + lib.onResume(activity); + } + + libs.add(lib); + } + + public void fireResumed() { + isResumed = true; + for (MDVRLibrary library : libs) { + library.onResume(activity); + } + } + + public void firePaused() { + isResumed = false; + for (MDVRLibrary library : libs) { + library.onPause(activity); + } + } + + public void fireDestroy() { + for (MDVRLibrary library : libs) { + library.onDestroy(); + } + } + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 00000000..7eb1449e --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,13 @@ + + + + + + diff --git a/app/src/main/res/layout/activity_md_using_surface_view.xml b/app/src/main/res/layout/activity_md_using_surface_view.xml index ebd369ff..8cd26aae 100644 --- a/app/src/main/res/layout/activity_md_using_surface_view.xml +++ b/app/src/main/res/layout/activity_md_using_surface_view.xml @@ -36,254 +36,224 @@ - - - - - - - - - - - - -