|
| 1 | +package com.asha.md360player4android; |
| 2 | + |
| 3 | +import android.content.ContentResolver; |
| 4 | +import android.content.res.Resources; |
| 5 | +import android.graphics.Bitmap; |
| 6 | +import android.graphics.drawable.Drawable; |
| 7 | +import android.net.Uri; |
| 8 | +import android.os.Bundle; |
| 9 | +import android.support.annotation.DrawableRes; |
| 10 | +import android.util.Log; |
| 11 | +import android.view.View; |
| 12 | +import android.widget.Toast; |
| 13 | + |
| 14 | +import com.asha.vrlib.MDVRLibrary; |
| 15 | +import com.asha.vrlib.model.MDRay; |
| 16 | +import com.asha.vrlib.plugins.hotspot.IMDHotspot; |
| 17 | +import com.asha.vrlib.texture.MD360BitmapTexture; |
| 18 | +import com.asha.vrlib.texture.MD360CubemapTexture; |
| 19 | +import com.squareup.picasso.Picasso; |
| 20 | +import com.squareup.picasso.Target; |
| 21 | + |
| 22 | +import static com.squareup.picasso.MemoryPolicy.NO_CACHE; |
| 23 | +import static com.squareup.picasso.MemoryPolicy.NO_STORE; |
| 24 | + |
| 25 | +/** |
| 26 | + * Created by hzqiujiadi on 16/4/5. |
| 27 | + |
| 28 | + */ |
| 29 | +public class CubemapPlayerActivity extends MD360PlayerActivity { |
| 30 | + |
| 31 | + private static final String TAG = "BitmapPlayerActivity"; |
| 32 | + |
| 33 | + private Uri nextUri = null; |
| 34 | + |
| 35 | + @Override |
| 36 | + public void onCreate(Bundle savedInstanceState) { |
| 37 | + super.onCreate(savedInstanceState); |
| 38 | + |
| 39 | + findViewById(R.id.control_next).setOnClickListener(new View.OnClickListener() { |
| 40 | + @Override |
| 41 | + public void onClick(View v) { |
| 42 | + busy(); |
| 43 | + //nextUri = getDrawableUri(R.drawable.texture); |
| 44 | + getVRLibrary().notifyPlayerChanged(); |
| 45 | + } |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + private Target mTarget;// keep the reference for picasso. |
| 50 | + |
| 51 | + private void loadImage(Uri uri, final MD360CubemapTexture.Callback callback){ |
| 52 | + mTarget = new Target() { |
| 53 | + @Override |
| 54 | + public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { |
| 55 | + Log.d(TAG, "loaded image, size:" + bitmap.getWidth() + "," + bitmap.getHeight()); |
| 56 | + |
| 57 | + // notify if size changed |
| 58 | + getVRLibrary().onTextureResize(bitmap.getWidth(), bitmap.getHeight()); |
| 59 | + |
| 60 | + // texture |
| 61 | + callback.texture(bitmap); |
| 62 | + //cancelBusy(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void onBitmapFailed(Drawable errorDrawable) { |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onPrepareLoad(Drawable placeHolderDrawable) { |
| 72 | + |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + Log.d(TAG, "load image with max texture size:" + callback.getMaxTextureSize()); |
| 77 | + Picasso.with(getApplicationContext()) |
| 78 | + .load(uri) |
| 79 | + .resize(callback.getMaxTextureSize(),callback.getMaxTextureSize()) |
| 80 | + .onlyScaleDown() |
| 81 | + .centerInside() |
| 82 | + .memoryPolicy(NO_CACHE, NO_STORE) |
| 83 | + .into(mTarget); |
| 84 | + } |
| 85 | + |
| 86 | + private Uri currentUri(){ |
| 87 | + if (nextUri == null){ |
| 88 | + return getUri(); |
| 89 | + } else { |
| 90 | + return nextUri; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + protected MDVRLibrary createVRLibrary() { |
| 96 | + return MDVRLibrary.with(this) |
| 97 | + .displayMode(MDVRLibrary.DISPLAY_MODE_NORMAL) |
| 98 | + .interactiveMode(MDVRLibrary.INTERACTIVE_MODE_TOUCH) |
| 99 | + .projectionMode(MDVRLibrary.PROJECTION_MODE_CUBE) // needed |
| 100 | + .asCubemap(new MDVRLibrary.ICubemapProvider() { |
| 101 | + @Override |
| 102 | + public void onProvideCubemap(MD360CubemapTexture.Callback callback, int cubeFace) { |
| 103 | + Log.d(TAG, "Load face: " + cubeFace); |
| 104 | + |
| 105 | + switch(cubeFace) { |
| 106 | + case MD360CubemapTexture.CUBE_FRONT: |
| 107 | + nextUri = getDrawableUri(R.drawable.cube_front); |
| 108 | + break; |
| 109 | + case MD360CubemapTexture.CUBE_BACK: |
| 110 | + nextUri = getDrawableUri(R.drawable.cube_back); |
| 111 | + break; |
| 112 | + case MD360CubemapTexture.CUBE_TOP: |
| 113 | + nextUri = getDrawableUri(R.drawable.cube_top); |
| 114 | + break; |
| 115 | + case MD360CubemapTexture.CUBE_BOTTOM: |
| 116 | + nextUri = getDrawableUri(R.drawable.cube_bottom); |
| 117 | + break; |
| 118 | + case MD360CubemapTexture.CUBE_LEFT: |
| 119 | + nextUri = getDrawableUri(R.drawable.cube_left); |
| 120 | + break; |
| 121 | + case MD360CubemapTexture.CUBE_RIGHT: |
| 122 | + nextUri = getDrawableUri(R.drawable.cube_right); |
| 123 | + break; |
| 124 | + default: |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + loadImage(currentUri(), callback); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void onReady() { |
| 133 | + // This can be used to hide a loading view and show the library view |
| 134 | + Toast.makeText(CubemapPlayerActivity.this, "CubeMap Ready", Toast.LENGTH_SHORT).show(); |
| 135 | + cancelBusy(); |
| 136 | + } |
| 137 | + }) |
| 138 | + .listenTouchPick(new MDVRLibrary.ITouchPickListener() { |
| 139 | + @Override |
| 140 | + public void onHotspotHit(IMDHotspot hitHotspot, MDRay ray) { |
| 141 | + Log.d(TAG,"Ray:" + ray + ", hitHotspot:" + hitHotspot); |
| 142 | + } |
| 143 | + }) |
| 144 | + .pinchEnabled(true) |
| 145 | + .build(findViewById(R.id.gl_view)); |
| 146 | + } |
| 147 | + |
| 148 | + private Uri getDrawableUri(@DrawableRes int resId){ |
| 149 | + Resources resources = getResources(); |
| 150 | + return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + resources.getResourcePackageName(resId) + '/' + resources.getResourceTypeName(resId) + '/' + resources.getResourceEntryName(resId) ); |
| 151 | + } |
| 152 | +} |
0 commit comments