簡體   English   中英

在屏幕方向更改時更改imageview image src

[英]Change imageview image src on screen orientation change

我正在制作一個簡單的android應用程序,該程序可在webview中加載網頁,並且在以URL加載開始時出現的imageview中有一個初始屏幕作為加載屏幕。 我的問題是我想根據屏幕方向加載不同的圖像,這是我的代碼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

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);
    }


}

我在同一文件夾中有用於景觀布局的另一張圖像,但如何加載呢?

您可以為每個方向創建不同的布局文件夾。

  • 縱向模式的layout-port
  • 用於景觀模式的layout-land

或者,如果您只想更改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
}

如Grender所述,您可以加載縱向和橫向的不同布局。 您也可以以編程方式使用。

在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

因此,如果您更改屏幕方向,則會再次調用onCreate方法,並且此代碼確定您的imageview應該使用哪個src。

編輯:感謝Grender的提示,在onCreate中您應該使用活動資源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM