2014-10-20 77 views
-1

我正在編寫書籍應用程序。我想在我的文本文件上添加縮放功能。在這裏我的代碼:從資源文件中放大文本文件中的「TextView」

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:orientation="vertical" > 

    <ScrollView 
     android:id="@+id/ScrollView01" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" > 

     <TextView 
      android:id="@+id/textview_data" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Naruto" 
      android:textSize="25dp" /> 
    </ScrollView> 

</LinearLayout> 

在MainActivity我申請一些MapView的縮放方法來解決這個問題,但這些方法並沒有太大幫助。

package com.example.readtextfiles; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.widget.TextView; 

public class ReadTextFileActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     TextView textView = (TextView)findViewById(R.id.textview_data); 


     String data = readTextFile(this, R.raw.books); 
     textView.setText(data); 

       } 

    public static String readTextFile(Context ctx, int resId) 
    { 
     InputStream inputStream = ctx.getResources().openRawResource(resId); 

     InputStreamReader inputreader = new InputStreamReader(inputStream); 
     BufferedReader bufferedreader = new BufferedReader(inputreader); 
     String line; 
     StringBuilder stringBuilder = new StringBuilder(); 
     try 
     { 
      while ((line = bufferedreader.readLine()) != null) 
      { 
       stringBuilder.append(line); 
       stringBuilder.append('\n'); 
      } 
     } 
     catch (IOException e) 
     { 
      return null; 
     } 
     return stringBuilder.toString(); 
    } 
} 

請提出一些幫助,

+0

可能重複([如何放大Android的一個TextView?] http://stackoverflow.com/questions/10239891/how-to-zoom-a-textview-in-android) – karvoynistas 2014-10-20 12:36:36

回答

0

只需添加兩個按鈕:加號和減號

加:

count = count +1; 

減:

count = count -1; 

    textView.setTextSize(count); 
0

您可以使用此代碼捏在Android的放大了的TextView://將這些XML(佈局)文件:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/mytv" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:text=" 
      This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks 
      This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks 
      This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks 
      This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks 
      This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks 
      This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks 

      This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks 
      This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks 
      This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks 
      This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks 
      This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks 
      This is my sample text for pinch zoom demo, you can zoom in and out using pinch zoom, thanks 
      " /> 

    </RelativeLayout> 

,並使用這個類PinchZoomOnTextview,

public class MyTextViewPinchZoomClass extends Activity implements OnTouchListener { 

    final static float STEP = 200; 
    TextView mytv; 
    float mRatio = 1.0f; 
    int mBaseDist; 
    float mBaseRatio; 
    float fontsize = 13; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout_file_name_here); 

     mytv = (TextView) findViewById(R.id.mytv); 
     mytv.setTextSize(mRatio + 13); 
    } 

    public boolean onTouchEvent(MotionEvent event) { 
     if (event.getPointerCount() == 2) { 
      int action = event.getAction(); 
      int pureaction = action & MotionEvent.ACTION_MASK; 
      if (pureaction == MotionEvent.ACTION_POINTER_DOWN) { 
       mBaseDist = getDistance(event); 
       mBaseRatio = mRatio; 
      } else { 
       float delta = (getDistance(event) - mBaseDist)/STEP; 
       float multi = (float) Math.pow(2, delta); 
       mRatio = Math.min(1024.0f, Math.max(0.1f, mBaseRatio * multi)); 
       mytv.setTextSize(mRatio + 13); 
      } 
     } 
     return true; 
    } 

    int getDistance(MotionEvent event) { 
     int dx = (int) (event.getX(0) - event.getX(1)); 
     int dy = (int) (event.getY(0) - event.getY(1)); 
     return (int) (Math.sqrt(dx * dx + dy * dy)); 
    } 

    public boolean onTouch(View v, MotionEvent event) { 
     return false; 
    } 
} 
+0

謝謝,但我想從我的資源文件夾的文本文件應用縮放。我在這裏問了問題之前試過了,但沒有幫助。 – 2014-10-20 12:49:32

相關問題