Ecco un esempio di come riuscire ad applicare lo zoom sopra una textview per applicativi android.
Una parte di codice è stata presa dalla guida How to use Multi-touch in Android.
La versione SDK minina richiesta per il funzionamento è la 11.
- layout xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:id="@+id/txtTitolo"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text=""
android:gravity="center"
android:textStyle="normal|italic" />
android:id="@+id/vertScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:id="@+id/lnyDescrizione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
android:id="@+id/txtDescrizionePrevisione"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="...loading..."
android:textAppearance="?android:attr/textAppearanceMedium"/>
- java
public class PrevisioneActivity extends Activity implements View.OnClickListener {
private TextView textViewPrevisione = null;
private TextView textView = null;
private TextView txtDescrizione = null;
private ScrollView vertScrollView = null;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
public static Boolean mTwoFingersTapped = false;
// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_previsione);
try {
// Gesture detection
PrevGestureDetector detect = new PrevGestureDetector();
detect.setActivity(this);
gestureDetector = new GestureDetector(this, detect);
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); }
};
textViewPrevisione = (TextView) findViewById(R.id.txtDescrizione);
textViewPrevisione.setOnClickListener(PrevisioneActivity.this);
textViewPrevisione.setOnTouchListener(gestureListener);
} catch (Exception ex) {
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event){
super.dispatchTouchEvent(event);
int action = event.getAction();
switch(action & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_POINTER_DOWN:
// set the mTwoFingersTapped flag to TRUE when we tap with 2 fingers at once
mTwoFingersTapped = true;
break;
}
// Dump touch event to log
dumpEvent(event);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG" );
mode = DRAG;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE" );
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
} else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM" );
}
break;
}
float[] f = new float[9];
matrix.getValues(f);
float scaleX = f[Matrix.MSCALE_X];
float scaleY = f[Matrix.MSCALE_Y];
float globalX = event.getX();
float globalY = event.getY();
PointF position = new PointF();
position.set(globalX, globalY);
if (mode == ZOOM ) { zoom(scaleX, scaleY, mid); } else {
if (mode == DRAG ) { zoom(scaleX, scaleY, position); }
}
txtDescrizione.setVerticalScrollBarEnabled(false);
return gestureDetector.onTouchEvent(event);
}
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
@Override
public void onClick(View v) { // Parameter v stands for the view that was clicked.
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
}
/** Show an event in the LogCat view, for debugging */
private void dumpEvent(MotionEvent event) {
String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,"POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("event ACTION_" ).append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
|| actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid " ).append(
action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")" );
}
sb.append("[" );
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#" ).append(i);
sb.append("(pid " ).append(event.getPointerId(i));
sb.append(")=" ).append((int) event.getX(i));
sb.append("," ).append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";" );
}
sb.append("]" );
sb.append("[TwoFingersTapped=" + mTwoFingersTapped + "]");
Log.d(TAG, sb.toString());
}
public void zoom(Float scaleX, Float scaleY, PointF pivot){
lnyDescrizionePrevisione.setPivotX(pivot.x);
//lnyDescrizionePrevisione.setPivotY(pivot.y);
lnyDescrizionePrevisione.setScaleX(scaleX);
lnyDescrizionePrevisione.setScaleY(scaleY);
}
class PrevGestureDetector extends GestureDetector.SimpleOnGestureListener {
PrevisioneActivity oActivity = null;
public void setActivity(PrevisioneActivity oActivityParam){ oActivity = oActivityParam; }
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (mTwoFingersTapped) {
//Toast.makeText(PrevisioneActivity.this, " Two Fingers Tapped Once. Yeeeyy
", Toast.LENGTH_SHORT).show();
}
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//Toast.makeText(PrevisioneActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//Toast.makeText(PrevisioneActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception ex) {
}
mTwoFingersTapped = false;
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
}
}
Per scaricare i file di esempio Zoom Text View
Una parte di codice è stata presa dalla guida How to use Multi-touch in Android.
La versione SDK minina richiesta per il funzionamento è la 11.
- layout xml
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text=""
android:gravity="center"
android:textStyle="normal|italic" />
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="...loading..."
android:textAppearance="?android:attr/textAppearanceMedium"/>
- java
public class PrevisioneActivity extends Activity implements View.OnClickListener {
private TextView textViewPrevisione = null;
private TextView textView = null;
private TextView txtDescrizione = null;
private ScrollView vertScrollView = null;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
public static Boolean mTwoFingersTapped = false;
// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_previsione);
try {
// Gesture detection
PrevGestureDetector detect = new PrevGestureDetector();
detect.setActivity(this);
gestureDetector = new GestureDetector(this, detect);
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); }
};
textViewPrevisione = (TextView) findViewById(R.id.txtDescrizione);
textViewPrevisione.setOnClickListener(PrevisioneActivity.this);
textViewPrevisione.setOnTouchListener(gestureListener);
} catch (Exception ex) {
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event){
super.dispatchTouchEvent(event);
int action = event.getAction();
switch(action & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_POINTER_DOWN:
// set the mTwoFingersTapped flag to TRUE when we tap with 2 fingers at once
mTwoFingersTapped = true;
break;
}
// Dump touch event to log
dumpEvent(event);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG" );
mode = DRAG;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE" );
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
} else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM" );
}
break;
}
float[] f = new float[9];
matrix.getValues(f);
float scaleX = f[Matrix.MSCALE_X];
float scaleY = f[Matrix.MSCALE_Y];
float globalX = event.getX();
float globalY = event.getY();
PointF position = new PointF();
position.set(globalX, globalY);
if (mode == ZOOM ) { zoom(scaleX, scaleY, mid); } else {
if (mode == DRAG ) { zoom(scaleX, scaleY, position); }
}
txtDescrizione.setVerticalScrollBarEnabled(false);
return gestureDetector.onTouchEvent(event);
}
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
@Override
public void onClick(View v) { // Parameter v stands for the view that was clicked.
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
}
/** Show an event in the LogCat view, for debugging */
private void dumpEvent(MotionEvent event) {
String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,"POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("event ACTION_" ).append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
|| actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid " ).append(
action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")" );
}
sb.append("[" );
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#" ).append(i);
sb.append("(pid " ).append(event.getPointerId(i));
sb.append(")=" ).append((int) event.getX(i));
sb.append("," ).append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";" );
}
sb.append("]" );
sb.append("[TwoFingersTapped=" + mTwoFingersTapped + "]");
Log.d(TAG, sb.toString());
}
public void zoom(Float scaleX, Float scaleY, PointF pivot){
lnyDescrizionePrevisione.setPivotX(pivot.x);
//lnyDescrizionePrevisione.setPivotY(pivot.y);
lnyDescrizionePrevisione.setScaleX(scaleX);
lnyDescrizionePrevisione.setScaleY(scaleY);
}
class PrevGestureDetector extends GestureDetector.SimpleOnGestureListener {
PrevisioneActivity oActivity = null;
public void setActivity(PrevisioneActivity oActivityParam){ oActivity = oActivityParam; }
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (mTwoFingersTapped) {
//Toast.makeText(PrevisioneActivity.this, " Two Fingers Tapped Once. Yeeeyy
", Toast.LENGTH_SHORT).show();}
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//Toast.makeText(PrevisioneActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
//Toast.makeText(PrevisioneActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception ex) {
}
mTwoFingersTapped = false;
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
}
}
Per scaricare i file di esempio Zoom Text View
Salsomaggiore Terme (Programmazione) - 25/08/2014 - Zoom textView with two fingers tapped android
Written by Mokik
Written by Mokik
Link referral
Tuttavia, tenete presente che i link referral non influenzano il nostro giudizio o il contenuto dell’articolo. Il nostro obiettivo è fornire sempre informazioni accurate, approfondite e utili per i nostri lettori. Speriamo che questi link referral non compromettano la vostra esperienza di navigazione e vi invitiamo a continuare a leggere i nostri articoli con fiducia, sapendo che il nostro impegno è offrirvi sempre il meglio.
Amazon Sostieni MrPaloma facendo acquisti su Amazon partendo da questo link amazon.it.
NordVpn Proteggi la tua navigazione e sostienici: acquista NordVPN tramite il link affiliato! Nord Vpn
Amazon Prime | Amazon Music Unlimited | Prime Video | Amazon Business | Kindle Unlimited | Amazon Wedding List | Prime Student