<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"
>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:drawableTop="@drawable/ic_launcher"
android:text="@string/hello_world" />
</RelativeLayout>
package com.example.viewcsc;
import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity {
private TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview=(TextView) findViewById(R.id.textview);
textview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
setClickZoomEffect(arg0);
}
});
}
public void setClickZoomEffect(final View view) {
if (view != null) {
view.setOnTouchListener(new OnTouchListener() {
boolean cancelled;
Rect rect = new Rect();
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
scaleTo(v, 1.2f);
break;
case MotionEvent.ACTION_MOVE:
if (rect.isEmpty()) {
v.getDrawingRect(rect);
}
if (!rect.contains((int) event.getX(), (int) event.getY())) {
scaleTo(v, 1);
cancelled = true;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
if (!cancelled) {
scaleTo(v, 1);
} else {
cancelled = false;
}
break;
}
}
return false;
}
});
}
}
public static void scaleTo(View v, float scale) {
if (Build.VERSION.SDK_INT >= 11) {
v.setScaleX(scale);
v.setScaleY(scale);
} else {
float oldScale = 1;
if (v.getTag(Integer.MIN_VALUE) != null) {
oldScale = (Float) v.getTag(Integer.MIN_VALUE);
}
LayoutParams params = v.getLayoutParams();
params.width = (int) ((params.width / oldScale) * scale);
params.height = (int) ((params.height / oldScale) * scale);
v.setTag(Integer.MIN_VALUE, scale);
}
}
}