- 之前搜了下看到一个类似的程序,稍微修改了下
-对需要放大的控件进行重写
-判断控件是否有焦点变化,有则放大动画,放大后图片最终缩小为原尺寸
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
第一次写,以前见过类似代码,但是找不到了,自己就稍微整理修改了下,写了个这个东西,以方便自己以后看。