缩小后查看未显示所有内容

时间:2013-06-08 16:47:21

标签: android canvas view zoom scale

使用View.setScaleX()View.setScaleY()方法缩小我的view后,结果只会显示已显示的内容,而其余部分则不会显示屏幕放大时。如何强制它显示其余内容?

XML:activity_main.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/table"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

JAVA:MainActivity.java

package com.example.zoom;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TableLayout table = (TableLayout) findViewById(R.id.table);

        int textViewSize = 100;
        int tableColCount = 15;
        int tableRowCount = 15;

        for (int row = 0; row < tableRowCount; row++) {
            TableRow tr = new TableRow(this);
            for (int col = 0; col < tableColCount; col++) {
                TextView textView = new TextView(this);
                textView.setText("" + row + "." + col);
                textView.setTextSize(20.0f);
                tr.addView(textView, textViewSize, textViewSize);
            } // for
            table.addView(tr);
        } // for
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        LinearLayout table = (LinearLayout) findViewById(R.id.table);

        switch (item.getItemId()) {

            case R.id.action_zoom_in:
                table.setScaleX(1.0f);
                table.setScaleY(1.0f);
                break;
            case R.id.action_zoom_out:
                table.setScaleX(0.25f);
                table.setScaleY(0.25f);
                break;
        } // switch
        return true;
    }
}

屏幕拍摄:Zoomed In | Zoomed Out

我注意到this question与我的非常相似,但没有代码也没有答案。

1 个答案:

答案 0 :(得分:0)

事实证明,手动设置view的大小(而不是使用wrap_content)就可以了。这就是真正需要的:

FrameLayout.LayoutParams tableLP = new FrameLayout.LayoutParams(
    textViewSize * tableColCount, textViewSize * tableRowCount);
table.setLayoutParams(tableLP);

我还将枢轴点设置为0以使视图保持在左上角。

table.setPivotX(0);
table.setPivotY(0);

关于pskink(我对原始问题发表评论),我调查了动画,发现使用ViewPropertyAnimator做了一件相当不错的工作。要使用此课程,您只需拨打View.animate()

设置将要缩放的view的大小以及view的轴心点后,您可以使用基本缩放方法而不使用动画,

// zoom in by 50%
float scale = 0.5f;
view.setScaleX(table.getScaleX() + scale);
view.setScaleY(table.getScaleY() + scale);

或带动画的缩放方法。

// zoom in by 50%
float scale = 0.5f;
table.animate().scaleXBy(scale).scaleYBy(scale).setDuration(0).start();

以下是原始问题中该程序的完整示例:

package com.example.zoom;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TableLayout table = (TableLayout) findViewById(R.id.table);

        int textViewSize = 100;
        int tableColCount = 15;
        int tableRowCount = 15;

        for (int row = 0; row < tableRowCount; row++) {
            TableRow tr = new TableRow(this);
            for (int col = 0; col < tableColCount; col++) {
                TextView textView = new TextView(this);
                textView.setText(row + "." + col);
                textView.setTextSize(20.0f);
                tr.addView(textView, textViewSize, textViewSize);
            } // for
            table.addView(tr);
        } // for

        // manually set the size to make sure the entire view is drawn and will be seen when scaled down
        FrameLayout.LayoutParams tableLP = new FrameLayout.LayoutParams(textViewSize * tableColCount, textViewSize * tableRowCount);
        table.setLayoutParams(tableLP);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        TableLayout table = (TableLayout) findViewById(R.id.table);
        // set default scale to 0 in case no scale occurs
        float scale = 0.0f;

        switch (item.getItemId()) {

            case R.id.action_zoom_in:
                scale = 0.5f;
                break;
            case R.id.action_zoom_out:
                // use negative to decrease the scale
                scale = -0.5f;
                break;
        } // switch

        // set pivot to 0 to keep view in the top left corner
        table.setPivotX(0);
        table.setPivotY(0);
        // add new scale with current scale to get View.scaleBy() equivalent
        table.setScaleX(table.getScaleX() + scale);
        table.setScaleY(table.getScaleY() + scale);
//      could use an animation instead
//      table.animate().scaleXBy(scale).scaleYBy(scale).setDuration(0).start();
        return true;
    }
}