我有Activity
只显示图片。我想在旋转手机时旋转图像。我已经阅读了几乎所有关于方向变化的SO帖子,这就是我所做的:
AlternateFullImageActivity.java
public class AlternateFullImageActivity extends me.imid.swipebacklayout.lib.app.SwipeBackActivity {
String API;
String photoId;
String token;
TinyDB tinyDB;
RestAdapter restAdapter;
@Bind(R.id.zoomable)
ImageView image;
PhotoViewAttacher mAttacher;
Bitmap bmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alternate_full_image);
SwipeBackLayout mSwipeBackLayout = getSwipeBackLayout();
mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_ALL);
ButterKnife.bind(this);
Log.i("PP", "AlternateFullImageActivity called");
Intent intent = getIntent();
photoId = intent.getExtras().getString("ID");
RequestInterceptor requestInterceptor = new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addHeader("Accept", "application/json");
request.addHeader("Authorization", "Token " + token);
}
};
OkHttpClient okHttpClient = new OkHttpClient();
restAdapter = new RestAdapter.Builder()
.setClient(new OkClient(okHttpClient))
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint(API)
.setRequestInterceptor(requestInterceptor)
.build();
PhotoAPI photoAPI = restAdapter.create(PhotoAPI.class);
photoAPI.getFeed(photoId, new Callback<ImageItem>() {
@Override
public void success(ImageItem imageItem, Response response) {
Picasso.with(AlternateFullImageActivity.this)
.load(imageItem.getPhotoUrl())
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
bmp = bitmap;
image.setImageBitmap(bmp);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
mAttacher = new PhotoViewAttacher(image);
}
@Override
public void failure(RetrofitError error) {
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("PP", "onConfigurationChanged called");
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
image.setImageBitmap(bmp);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
image.setImageBitmap(bmp);
}
}
activity_alternate_full_image.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context="com.galleri5.android.activities.AlternateFullImageActivity">
<uk.co.senab.photoview.PhotoView
android:id="@+id/zoomable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</RelativeLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.galleri5.android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission android:name="com.galleri5.android.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.galleri5.android.permission.C2D_MESSAGE" />
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@style/AppTheme"
tools:replace="android:icon">
<activity
android:name=".activities.LaunchActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.FullImageActivity"
android:screenOrientation="portrait"></activity>
<activity
android:name=".activities.AlternateFullImageActivity"
android:configChanges="layoutDirection|uiMode|screenSize|orientation">
</activity>
</application>
问题是即使在旋转手机时也根本没有调用onConfigurationChanged()
。为了检查问题所在,我创建了一个新项目,只有一个活动显示一个图像,并在清单文件中添加相同的配置标志,并在那里工作。我无法弄清问题是什么以及如何解决它。
修改
我注意到另一件事,当我在清单文件中为我的活动添加android:screenOrientation="sensor"
时,图片会在旋转手机时旋转,但会调用onCreate()
以及onConfigurationChanged()
每次我旋转手机。现在假设这个活动用C表示,那么此时活动后栈是A-B-C。每次我旋转手机时,A的onCreate
也会被调用,这对我来说非常奇怪。我真的没有想法。我绝对不希望再次调用任何活动onCreate
。任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
如果我是你,我会采取以下方法。 (我没有测试过这段代码,但它应该给你指示如何继续)
如果您实施以下方法,则无需担心添加configChanges并手动处理方向更改。另外,建议不要使用onConfigurationChanged。
使用onSaveInstanceState
在活动娱乐中保留网址,并使用onCreate,如下所示。
private static final String KEY_URL = "url";
private String mUrl = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null && savedInstanceState.getString(KEY_URL) != null) {
mUrl = savedInstanceState.getString(KEY_URL);
loadImage(mUrl);
} else {
fetchNetworkUrl();
}
}
private void fetchNetworkUrl() {
// Service call and call following when you receive the url.
photoAPI.getFeed(photoId, new Callback<ImageItem>() {
@Override
public void success(ImageItem imageItem, Response response) {
mUrl = imageItem.getPhotoUrl();
loadImage(mUrl);
}
}
}
private void loadImage(String url){
// Use Picasso to load Image
}
在
之后的活动娱乐使用中保留网址@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString(KEY_URL, mUrl);
super.onSaveInstanceState(outState);
}