cheli_71 2013-09-05 08:07 采纳率: 0%
浏览 2333
已采纳

动态的删除一个图像出现的问题

程序中的 oncreate 方法

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

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

     getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    controlInflater = LayoutInflater.from(getBaseContext());
    viewControl = controlInflater.inflate(R.layout.control, null);
    image =(ImageView) viewControl.findViewById(R.id.img);
    LayoutParams layoutParamsControl 
        = new LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.FILL_PARENT);
    this.addContentView(viewControl, layoutParamsControl);
}

control.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
        >
<ImageView
    android:id="@+id/img"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/ic_launcher"
    />

 <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/button"
/>

</LinearLayout>

如何通过点击按钮来删除这个图像。实际上我想从当前的像素位置把这个图像转移到别的位置。

我点击按钮来实现:

b =(Button)findViewById(R.id.button);
        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) { 
                System.out.println("Button clicked");
                TranslateAnimation anim = new TranslateAnimation(0,0,200,200);              
                anim.setDuration(2000);
                image.setAnimation(anim); 
            }
        });

但是现在图像会消失一俩秒钟。哪里出错了呢是?事实上我想在 accelerometer 改变的时候移动图像。

  • 写回答

1条回答

  • longhai326 2013-09-06 03:38
    关注

    先写一个xml文件放在drawable目录下的anim目录,没有自己新建translate.xml
    <?xml version="1.0" encoding="utf-8"?>

    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toXDelta="200%p" />
    

    //在代码中
    Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
    // 开始执行动画
    image.startAnimation(animation);

    如果想要去掉ImageView,需要先执行 image.clearAnimation();然后将image.setVisibility(View.GONE);
    或者设置 image.setVisibility(View.INVISIBLE);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler