如何在android上设置整个布局的imageView动画?
我正在尝试将ImageView从子布局(Relativelayout)设置为父布局(Relativelayout)。进入父布局时未绘制imageview如何在android上设置整个布局的imageView动画?,android,android-layout,Android,Android Layout,我正在尝试将ImageView从子布局(Relativelayout)设置为父布局(Relativelayout)。进入父布局时未绘制imageview enter code /** Called when the activity is first created. */ private static final int SWIPE_MIN = 20; private static final int SWIPE_MAX_OFF_PATH = 250; p
enter code
/** Called when the activity is first created. */
private static final int SWIPE_MIN = 20;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD = 20;
GestureDetector detector;
ImageView topImage;
ImageView bottomImage;
ImageView aView;
RelativeLayout rlBottomChild;
RelativeLayout rlTopChild;
FrameLayout flTopChild;
FrameLayout flBottomChild;
RelativeLayout table;
RelativeLayout relativeLayout ;
FrameLayout frameLayout ;
WindowManager wManger;
int screenWidth;
int screenHeight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wManger = getWindowManager();
screenWidth = wManger.getDefaultDisplay().getWidth();
screenHeight = wManger.getDefaultDisplay().getHeight();
detector = new GestureDetector(this);
bottomImage = new ImageView(this);
bottomImage.setImageResource(R.drawable.icon);
bottomImage.setOnTouchListener(this);
bottomImage.setDrawingCacheEnabled(true);
topImage = new ImageView(this);
topImage.setImageResource(R.drawable.icon);
topImage.setOnTouchListener(this);
topImage.setDrawingCacheEnabled(true);
initRelativeLayout();
setContentView(relativeLayout);
}
/**
* <p> Used to </p>
*/
private void initRelativeLayout() {
// Top Player
rlTopChild = new RelativeLayout(this);
rlTopChild.setId(1);
LayoutParams p2 = new LayoutParams(LayoutParams.FILL_PARENT , ((10*screenHeight)/100));
p2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlTopChild.setBackgroundColor(Color.BLUE);
rlTopChild.addView(topImage);
rlTopChild.setLayoutParams(p2);
// Bottom Player
rlBottomChild = new RelativeLayout(this);
rlBottomChild.setId(3);
LayoutParams p = new LayoutParams(LayoutParams.FILL_PARENT , ((10*screenHeight)/100));
p.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rlBottomChild.setBackgroundColor(Color.BLUE);
rlBottomChild.addView(bottomImage);
rlBottomChild.setClipChildren(false);
rlBottomChild.setLayoutParams(p);
// Table Layout
table = new RelativeLayout(this);
table.setId(2);
LayoutParams p3 = new LayoutParams(LayoutParams.FILL_PARENT , ((50*screenHeight)/100));
p3.addRule(RelativeLayout.CENTER_IN_PARENT);
p3.addRule(RelativeLayout.BELOW , rlTopChild.getId());
p3.addRule(RelativeLayout.ABOVE , rlBottomChild.getId());
table.setBackgroundColor(Color.WHITE);
table.setLayoutParams(p3);
relativeLayout =new RelativeLayout(this);
relativeLayout.setBackgroundColor(Color.GREEN);
relativeLayout.addView(rlBottomChild);
relativeLayout.addView(rlTopChild);
relativeLayout.addView(table);
}
/**
* <p> Overriding the method </p>
* @param v
* @param event
* @return
* @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
*/
@Override
public boolean onTouch(View v, MotionEvent event) {
return detector.onTouchEvent(event);
}
/**
* <p> Overriding the method </p>
* @param e
* @return
* @see android.view.GestureDetector.OnGestureListener#onDown(android.view.MotionEvent)
*/
@Override
public boolean onDown(MotionEvent e) {
// TODOAuto-generated method stub
return true;
}
/**
* <p> Overriding the method </p>
* @param e1
* @param e2
* @param velocityX
* @param velocityY
* @return
* @see android.view.GestureDetector.OnGestureListener#onFling(android.view.MotionEvent, android.view.MotionEvent, float, float)
*/
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH){
return false;
}
if (e1.getX() - e2.getX() > SWIPE_MIN && Math.abs(velocityX) > SWIPE_THRESHOLD) {
debug("Left Swipe");
TranslateAnimation animation= new TranslateAnimation(e1.getX(),e2.getX(),e1.getY() ,e2.getY());
animation.setAnimationListener(this);
animation.setDuration(3000);
bottomImage.startAnimation(animation);
} else if (e2.getX() - e1.getX() > SWIPE_MIN && Math.abs(velocityX) > SWIPE_THRESHOLD) {
debug("Right Swipe ");
debug("Swipe down ...");
TranslateAnimation animation= new TranslateAnimation(e1.getX(),e2.getX(),e1.getY() ,e2.getY());
animation.setAnimationListener(this);
animation.setDuration(3000);
topImage.startAnimation(animation);
} else if (e1.getY() - e2.getY() > SWIPE_MIN && Math.abs(velocityX) > SWIPE_THRESHOLD) {
debug("Swipe up ... ");
TranslateAnimation animation= new TranslateAnimation(e1.getX(),e2.getX(),e1.getY() ,e2.getY());
animation.setAnimationListener(this);
animation.setDuration(3000);
bottomImage.startAnimation(animation);
} else if (e2.getY() - e1.getY() > SWIPE_MIN && Math.abs(velocityX) > SWIPE_THRESHOLD) {
debug("Swipe down ...");
TranslateAnimation animation= new TranslateAnimation(e1.getX(),e2.getX(),e1.getY() ,e2.getY());
animation.setAnimationListener(this);
animation.setDuration(3000);
topImage.startAnimation(animation);
}
} catch (Exception e) {
// nothing
}
return true;
}
/**
* <p> Used to </p>
* @param string
*/
private void debug(String string) {
Log.d("TAGGG", string);
}
/**
* <p> Overriding the method </p>
* @param e
* @see android.view.GestureDetector.OnGestureListener#onLongPress(android.view.MotionEvent)
*/
@Override
public void onLongPress(MotionEvent e) {
// TODOAuto-generated method stub
}
/**
* <p> Overriding the method </p>
* @param e1
* @param e2
* @param distanceX
* @param distanceY
* @return
* @see android.view.GestureDetector.OnGestureListener#onScroll(android.view.MotionEvent, android.view.MotionEvent, float, float)
*/
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// TODOAuto-generated method stub
return true;
}
/**
* <p> Overriding the method </p>
* @param e
* @see android.view.GestureDetector.OnGestureListener#onShowPress(android.view.MotionEvent)
*/
@Override
public void onShowPress(MotionEvent e) {
// TODOAuto-generated method stub
}
/**
* <p> Overriding the method </p>
* @param e
* @return
* @see android.view.GestureDetector.OnGestureListener#onSingleTapUp(android.view.MotionEvent)
*/
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODOAuto-generated method stub
return true;
}
/**
* <p> Overriding the method </p>
* @param animation
* @see android.view.animation.Animation.AnimationListener#onAnimationEnd(android.view.animation.Animation)
*/
@Override
public void onAnimationEnd(Animation animation) {
// TODOAuto-generated method stub
}
/**
* <p> Overriding the method </p>
* @param animation
* @see android.view.animation.Animation.AnimationListener#onAnimationRepeat(android.view.animation.Animation)
*/
@Override
public void onAnimationRepeat(Animation animation) {
// TODOAuto-generated method stub
}
/**
* <p> Overriding the method </p>
* @param animation
* @see android.view.animation.Animation.AnimationListener#onAnimationStart(android.view.animation.Animation)
*/
@Override
public void onAnimationStart(Animation animation) {
}
输入代码
/**在首次创建活动时调用*/
专用静态最终整数滑动最小值=20;
专用静态最终整数滑动路径=250;
专用静态最终整数滑动\u阈值=20;
手势检测器;
图像视图topImage;
图像视图;
图像视图视图;
亲子关系;
相对年轻的孩子;
框架布局flTopChild;
框架布局;子对象;
相对表;
相对的相对的;
框架布局框架布局;
窗口管理器;
int屏幕宽度;
屏幕高度;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
wManger=getWindowManager();
screenWidth=wManger.getDefaultDisplay().getWidth();
screenHeight=wManger.getDefaultDisplay().getHeight();
检测器=新的手势检测器(本);
bottomImage=新图像视图(此);
setImageResource(R.drawable.icon);
setOnTouchListener(这个);
bottomImage.setDrawingCacheEnabled(true);
topImage=新的图像视图(此);
setImageResource(R.drawable.icon);
setOnTouchListener(这个);
topImage.setDrawingCacheEnabled(true);
initRelativeLayout();
setContentView(relativeLayout);
}
/**
*习惯于
*/
私有void initRelativeLayout(){
//顶尖选手
rlTopChild=新的RelativeLayout(此);
rlTopChild.setId(1);
LayoutParams p2=新的LayoutParams(LayoutParams.FILL_父项,((10*屏幕高度)/100));
p2.添加规则(相对长度对齐父项对齐顶部);
rlTopChild.setBackgroundColor(Color.BLUE);
rlTopChild.addView(topImage);
rlTopChild.setLayoutParams(p2);
//底层玩家
rlBottomChild=新的RelativeLayout(此);
rlBottomChild.setId(3);
LayoutParams p=新的LayoutParams(LayoutParams.FILL_父项,((10*屏幕高度)/100));
p、 addRule(相对长度对齐父项底部);
rlBottomChild.setBackgroundColor(Color.BLUE);
rlBottomChild.addView(bottomImage);
rlBottomChild.setClipChildren(false);
rlBottomChild.setLayoutParams(p);
//表格布局
表=新的相对长度(本);
表2.setId(2);
LayoutParams p3=新的LayoutParams(LayoutParams.FILL_父项,((50*屏幕高度)/100));
p3.添加规则(父项中的相对位置中心);
p3.addRule(RelativeLayout.down,rlTopChild.getId());
p3.addRule(RelativeLayout.over,rlBottomChild.getId());
表.立根基色(白色);
表.setLayoutParams(p3);
relativeLayout=新的relativeLayout(本);
相对退根基色(颜色.绿色);
relativeLayout.addView(rlBottomChild);
relativeLayout.addView(rlTopChild);
relativeLayout.addView(表);
}
/**
*重写该方法
*@param v
*@param事件
*@返回
*@see android.view.view.OnTouchListener#onTouch(android.view.view,android.view.MotionEvent)
*/
@凌驾
公共布尔onTouch(视图v,运动事件){
返回检测器。开孔(事件);
}
/**
*重写该方法
*@param e
*@返回
*@see android.view.GestureDetector.OnGetureListener#onDown(android.view.MotionEvent)
*/
@凌驾
公共布尔onDown(运动事件e){
//TODOAuto生成的方法存根
返回true;
}
/**
*重写该方法
*@param e1
*@param e2
*@param-velocityX
*@param-velocityY
*@返回
*@see android.view.GestureDetector.OnGetureListener#onFling(android.view.MotionEvent,android.view.MotionEvent,float,float)
*/
@凌驾
公共布尔onFling(MotionEvent e1、MotionEvent e2、float-velocityX、float-velocityY){
试一试{
if(Math.abs(e1.getY()-e2.getY())>swip\u MAX\u OFF\u路径){
返回false;
}
if(e1.getX()-e2.getX()>SWIPE\u MIN&&Math.abs(velocityX)>SWIPE\u THRESHOLD){
调试(“左扫”);
TranslateAnimation动画=新的TranslateAnimation(e1.getX(),e2.getX(),e1.getY(),e2.getY());
setAnimationListener(this);
动画。设置持续时间(3000);
bottomImage.startAnimation(动画);
}else if(e2.getX()-e1.getX()>SWIPE\u MIN&&Math.abs(velocityX)>SWIPE\u THRESHOLD){
调试(“右击”);
调试(“向下滑动…”);
TranslateAnimation动画=新的TranslateAnimation(e1.getX(),e2.getX(),e1.getY(),e2.getY());
setAnimationListener(this);
动画。设置持续时间(3000);
topImage.startAnimation(动画);
}else if(e1.getY()-e2.getY()>SWIPE\u MIN&&Math.abs(velocityX)>SWIPE\u THRESHOLD){
调试(“向上滑动…”);
TranslateAnimation动画=新的TranslateAnimation(e1.getX(),e2.getX(),e1.getY(),e2.getY());
setAnimationListener(this);
动画。设置持续时间(3000);
bottomImage.startAnimation(动画);
}else if(e2.getY()-e1.getY()>SWIPE\u MIN&&Math.abs(velocityX)>SWIPE\u THRESHOLD){
调试(“向下滑动…”);
TranslateAnimation动画=新的TranslateAnimation(e1.getX(),e2.getX(),e1.getY(),e2.getY());
setAnimationListener(this);
动画。设置持续时间(3000);
topImage.startAnimation(动画);
}
}捕获(例外e){
//没什么
}
返回true;
}
/**
*习惯于
*@param字符串
*/
私有void调试(字符串){
Log.d(“TAGGG”,字符串);
}
/**
*重写该方法
*@param e
*@see android.view.GestureDetector.OnGetureListener#onLongPress(android.view.MotionEvent)
*/
@凌驾
公开无效在线新闻(运动事件e){
//TODOAuto生成的方法存根
}
/**
*重写该方法
*@param e1
*@par
relativeLayout.setClipChildren(false);
rlBottomChild.setClipChildren(false);
rlTopChild.setClipChildren(false);
animation.setFillAfter(true);