[英]Change imageview image src on screen orientation change
I'm making a simple android app that loads a webpage in a webview and there is a splash screen as a loading screen in an imageview that appears at start with url loading. 我正在制作一个简单的android应用程序,该程序可在webview中加载网页,并且在以URL加载开始时出现的imageview中有一个初始屏幕作为加载屏幕。 My problem is that I want to load different images according to the screen orientation here is my code activity_main.xml
我的问题是我想根据屏幕方向加载不同的图像,这是我的代码activity_main.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:orientation="vertical"
tools:context="com.example.example.MainActivity">
<ImageView android:id="@+id/imageLoading1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:visibility="visible"
android:src="@mipmap/vert_loading" />
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
</RelativeLayout>
MainActivity.java MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("https://www.example.com/");
mWebView.setWebViewClient(new MyAppWebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
}
});
}
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
}
I have another image for the landscape layout in the same folder but how to load it ? 我在同一文件夹中有用于景观布局的另一张图像,但如何加载呢?
You can create different layout folders for each orientation. 您可以为每个方向创建不同的布局文件夹。
layout-port
for portrait mode. layout-port
。 layout-land
for landscape mode. layout-land
。 Or, if you want to change only the image src
you can detect the orientation changed event inside your Activity
and load the new image. 或者,如果您只想更改
image src
,则可以在Activity
检测到方向更改的事件并加载新图像。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
// Set landscape image src
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
// Set portrait image src
}
As Grender described you can load different layouts for portrait and landscape. 如Grender所述,您可以加载纵向和横向的不同布局。 You can also use the programmatically way.
您也可以以编程方式使用。
Set the imageview src in your onCreate method and use. 在onCreate方法中设置imageview src并使用。
int orientation = Activity.getResources().getConfiguration().orientation
if (orientation == Configuration.ORIENTATION_LANDSCAPE)
// Set landscape image src
else if (orientation == Configuration.ORIENTATION_PORTRAIT){
// Set portrait image src
So if you change your screen orientation, the onCreate method is called again and this code decides which src your imageview should use. 因此,如果您更改屏幕方向,则会再次调用onCreate方法,并且此代码确定您的imageview应该使用哪个src。
Edit: Thanks to Grender for the hint that in onCreate you should use your activity resources. 编辑:感谢Grender的提示,在onCreate中您应该使用活动资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.