Android view点击放大缩小

<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);  
       }  
 }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值