【问题标题】:How to zoom in/zoom out by increasing a layout or textView text size in **android**?如何通过增加 **android** 中的布局或 textView 文本大小来放大/缩小?
【发布时间】:2012-03-06 07:57:48
【问题描述】:

这是下面的代码,我想通过使用 android 的放大和缩小按钮来实现放大和缩小功能。

public class ShowDescription extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
    new LoadDetails(this).execute();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.showdescription);
    String theStory = null;
    String pubDate  = null;
    String storyTitle  = null;
    String writer  = null;
    //String test = null;

    Intent intent = getIntent();

    if (intent != null) {
        Bundle b = intent.getExtras();
        if (b == null) {
            theStory = "bad bundle?";
        } else {
            storyTitle =ComplexScript.UTF2ANSI(ComplexScript.swap(b.getString("title")))+"\n\n";
            //pubDate=ComplexScript.UTF2ANSI(ComplexScript.swap(ComplexScript.Replace(b.getString("pubdate"))))+ "\n\n";
            pubDate=ComplexScript.Replace(b.getString("pubdate"))+"\n";
            writer=ComplexScript.UTF2ANSI(ComplexScript.swap(b.getString("writers")))+",\n"+ComplexScript.UTF2ANSI(ComplexScript.swap(b.getString("initial")))+"\n";
            theStory =ComplexScript.UTF2ANSI(ComplexScript.swap(b.getString("description"))).replace('\n', ' ').replaceAll(" "," ").replaceAll("‘","Ô").replaceAll("’","Õ");
                    //+ "\n\nMore information:\n" + b.getString("link");
            //test=b.getString("image");
        }
    } else {
        theStory = "Information not found.";
    }
    //Bitmap bimage=  getBitmapFromURL(test);
    //iv.setImageBitmap(bimage);
    Typeface tf = Typeface.createFromAsset(getAssets(),
                "font/SutonnyMJ.ttf");
    TextView story = (TextView)findViewById(R.id.storybox);
    story.setText(Html.fromHtml("<h1><font color='#B10000'>"+storyTitle+"</font></h1><small>"+pubDate+"<br/>"+writer+"</small><p>"+theStory+"</p>"));
    story.setTypeface(tf);
    story.setLineSpacing(1,(float) 1.5);
    TextView moto =(TextView)findViewById(R.id.moto);
    moto.setText("msev` we‡bv`b mviv¶Y");
    moto.setTypeface(tf);
    Button backButton = (Button)findViewById(R.id.back);
    backButton.setTypeface(tf);
    TextView tv = (TextView) findViewById(R.id.footer);
    tv.setTypeface(tf);
    backButton.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v) {
            finish();
        }
    });
}}

下面是 XML 文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFF" android:orientation="vertical">
<ImageView  android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#86878A"
            android:contentDescription="@string/app_name"
            android:src="@drawable/logobn24"/>

<TextView   android:id="@+id/moto"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#86878A"
            android:textColor="#FFF"
            android:gravity="center"/>

<TextView   android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#86878A"/>

    <ScrollView android:id="@+id/scrollView1"
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="1">      

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#FFF"
            android:orientation="vertical" >

                <!-- android:textColor="#4e4e4e" -->
            <ImageView  android:id="@+id/img"
                        android:layout_width="150dp"
                        android:layout_height="100dp"
                        android:layout_alignParentTop="true"
                        android:layout_centerHorizontal="true"
                        android:contentDescription="@string/des"/>

            <TextView   android:id="@+id/storybox"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_below="@+id/img"
                        android:background="#FFFFFF"
                        android:paddingLeft="10dp"
                        android:textColor="#000"
                        android:textSize="18dp"
                        android:paddingTop="10dp"/>

        </RelativeLayout>
    </ScrollView>

<Button android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/back"/>

<TextView   android:id="@+id/footer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#86878A"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/footer"
            android:textColor="#FFF" android:lineSpacingMultiplier="1.3"/>

任何人都可以放大和缩小这个班级的设施吗?或者只是增加或减少文本视图 story 的文本大小? 谁能帮帮我?

【问题讨论】:

    标签: java android eclipse android-layout textview


    【解决方案1】:

    如果调用TextView的方法会发生什么?

    public void setScaleX(float scaleX)
    public void setScaleY(float scaleY)
    

    【讨论】:

    • 什么也没发生。因为,我的模拟器设备使用的是 android 2.2 和 API 级别 8。
    【解决方案2】:

    这可能对你有帮助...

     import android.content.Context;
     import android.graphics.Canvas;
     import android.util.AttributeSet;
     import android.view.MotionEvent;
     import android.view.ScaleGestureDetector;
     import android.widget.TextView;
    
    public class ResizableTextViewW extends TextView {
    private static final int INVALID_POINTER_ID = -1;
    
    public static final String TAG = ResizableTextViewW.class.getSimpleName();
    
    private float mLastTouchX;
    private float mLastTouchY;
    private int mActivePointerId = INVALID_POINTER_ID;
    
    private ScaleGestureDetector mScaleDetector;
    private float mScaleFactor = 1.f;
    private int count = 0;
    public float mPreviousScaleFactor = 1.f;
    
    public ResizableTextViewW(Context context) {
        this(context, null, 0);
    
    }
    
    public ResizableTextViewW(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    
    public ResizableTextViewW(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
    
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // Let the ScaleGestureDetector inspect all events.
        mScaleDetector.onTouchEvent(ev);
        return true;
    }
    
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    
    
        this.setText(String.valueOf(count));
        this.setTextSize(10+count);
    
    }
    
    private class ScaleListener extends
            ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFactor *= detector.getScaleFactor();
            // Don't let the object get too small or too large.
            mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
    
            if (mScaleFactor > mPreviousScaleFactor) {
                count = count + 1;
                invalidate();
    
            } else if (mScaleFactor < mPreviousScaleFactor) {
                if (count > 0) {
                    count = count - 1;
                    invalidate();
                }
            }
            mPreviousScaleFactor = mScaleFactor;
            return true;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-06
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      相关资源
      最近更新 更多