安卓控件获取焦点实现放大效果

  • 之前搜了下看到一个类似的程序,稍微修改了下

-对需要放大的控件进行重写
-判断控件是否有焦点变化,有则放大动画,放大后图片最终缩小为原尺寸

1.重写控件(此处重写了LinearLayout)

public class MyLinear extends LinearLayout {
private Animation scaleToLargeAnimation = null;
private int animationDuration = 100;
private int animationDelay = 1;

public MyLinear(Context context) {
    super(context);
    initAnimation();
}

public MyLinear(Context context, AttributeSet attrs) {
    super(context, attrs);
    initAnimation();
}


private void initAnimation() {

    scaleToLargeAnimation = AnimationUtils.loadAnimation(this.getContext(),
            R.anim.scale_to_large);

}

@Override
protected void onFocusChanged(boolean focused, int direction,
        Rect previouslyFocusedRect) {
    super.onFocusChanged(focused, direction, previouslyFocusedRect);

    if (focused) {
        scaleToLarge();
    } else {
        this.clearAnimation();
    }

}

private void scaleToLarge() {
    if (animationDuration > 0) {
        clearAnimation();
        scaleToLargeAnimation.setStartTime(AnimationUtils
                .currentAnimationTimeMillis() + animationDelay);
        setAnimation(scaleToLargeAnimation);
    }
}

}

2.动画特效

3.在xml中添加引用

   <com.xiaxl.MyLinear

       android:id="@+id/LL1"
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:orientation="vertical"
       android:layout_gravity="center_horizontal"
       android:layout_marginTop="15dp">

        <com.xiaxl.MyImageView 
            android:id="@+id/pic1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/tuisong"
            android:layout_gravity="center_horizontal"
            />
        <TextView 
            android:text="龙小二"
            android:textSize="20sp"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginTop="5dp"
             android:layout_gravity="center_horizontal"
            />
此处有一个图标,一个文本框

4.点击的时候获取焦点

MyLinear Linear;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Linear = (MyLinear)findViewById(R.id.LL1);
    Linear.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Linear.setFocusable(false);
            Linear.setFocusableInTouchMode(true);
            Linear.requestFocus();
            Linear.requestFocusFromTouch();
        }
    });
}

这样就可以在点击的时候放大整个LinearLayout

没放大的图片

放大后。。。效果太差了,不会做动画

第一次写,以前见过类似代码,但是找不到了,自己就稍微整理修改了下,写了个这个东西,以方便自己以后看。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值