使用动画从底部向上移动图像时,无法获得完整图像

时间:2013-11-14 08:47:24

标签: android android-animation

当用动画从底部向上移动图像时,没有得到完整的图像,我宣布图像视图宽度换行内容。当我向下移动时它的工作完美,但是当我拖动图像时,显示部分随附动画没有得到完整的图像。

        public class MainActivity extends Activity {

            // mainLayout is the child of the HorizontalScrollView ...
            private LinearLayout mainLayout;

            // this is an array that holds the IDs of the drawables ...
            private int[] images = { R.drawable.gg, R.drawable.gg, R.drawable.gg,
                    R.drawable.gg, R.drawable.gg, R.drawable.gg };

            int status = 0;
            int Measuredwidth = 0, MeasuredHeight = 0;
            float x = 0;
            boolean first = true;
            ImageButton btn;
            View cell = null;
            Matrix matrix = new Matrix();
            Matrix savedMatrix = new Matrix();
            PointF startPoint = new PointF();

            static final int NONE = 0;
            static final int DRAG = 1;
            Bitmap icon;

            int mode = NONE;
            AnimationListener animx;

            /** Called when the activity is first created. */
            @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
            @Override
            public void onCreate(Bundle icicle) {
                super.onCreate(icicle);

                setContentView(R.layout.activity_main);

                mainLayout = (LinearLayout) findViewById(R.id.linearLayout);

                for (int i = 0; i < images.length; i++) {

                    cell = getLayoutInflater().inflate(R.layout.image, null);
                    final ImageView img = (ImageView) cell.findViewById(R.id.imageView);

                    img.setImageResource(images[i]);

                    // mainLayout.addView(cell);
                    // }
                    final LinearLayout ll = (LinearLayout) cell
                            .findViewById(R.id.lllayout);

                    Point size = new Point();
                    WindowManager w = getWindowManager();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
                        w.getDefaultDisplay().getSize(size);
                        MeasuredHeight = size.y;
                        Measuredwidth = size.x;
                    } else {
                        Display d = w.getDefaultDisplay();
                        Measuredwidth = d.getWidth();
                        MeasuredHeight = d.getHeight();
                    }

                    // final AbsoluteLayout aalayout = (AbsoluteLayout) cell
                    // .findViewById(R.id.absLayout);

                    btn = (ImageButton) cell.findViewById(R.id.button1);
                    btn.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            Toast.makeText(getApplicationContext(), "button 1 pressed",
                                    2000).show();

                        }
                    });

                    img.setOnTouchListener(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(final View v, MotionEvent event) {

                            if (event.getAction() == MotionEvent.ACTION_UP) {
                                status = 0;

                                System.out.println("sdhsjdkklkl");

                            } else if (event.getAction() == MotionEvent.ACTION_DOWN) {

                                if (first) {
                                    x = event.getX();
                                    first = false;
                                    System.out.println("matrix: " + matrix);

                                    savedMatrix.set(matrix);
                                    mode = DRAG;
                                    startPoint.set(img.getLeft(), event.getY());
                                } else {

                                    startPoint.set(img.getLeft(), event.getY());
                                }
                            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                                float dx = event.getX() - startPoint.x;
                                final float dy = event.getY() - startPoint.y;

                                float setheight = btn.getBottom() + 6;

                                if (dy > 0) {

                                    System.out.println("saved matrix:" + savedMatrix);

                                    matrix.set(savedMatrix);

                                    System.out.println("y value: " + dy);
                                    matrix.postTranslate(img.getLeft(), 80);

                                    ll.setVisibility(View.VISIBLE);
                                    img.setImageMatrix(matrix);

                                }

                                else if (dy < 0) {

                                    int fromLoc[] = new int[2];
                                    img.getLocationOnScreen(fromLoc);
                                    final float startX = fromLoc[0];
                                    final float startY = fromLoc[1];

                                    int toLoc[] = new int[2];

                                    img.getLocationOnScreen(toLoc);
                                    final float destX = 0;
                                    final float destY = img.getTop();
                                    ll.setVisibility(View.GONE);

                                    TranslateAnimation mAnimation = new TranslateAnimation(
                                            0, 0, 0, -80);
                                    mAnimation.setDuration(10000);
                                    mAnimation.setRepeatCount(0);

                                    mAnimation
                                            .setInterpolator(new LinearInterpolator());
                                    mAnimation.setFillAfter(true);

                                    img.setAnimation(mAnimation);


                                }

                            }

                            return true;
                        }
                    });
                    System.out.println("bmgbgklb");
                    mainLayout.addView(cell);
                }
            }
        }

我的主要xml是:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dip" >

            <LinearLayout
                android:id="@+id/linearLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
            </LinearLayout>
        </HorizontalScrollView>


    </RelativeLayout>

我的子xml是:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginRight="2dip"
            android:scaleType="matrix"
            android:src="@drawable/dd" >
        </ImageView>

        <LinearLayout
            android:id="@+id/lllayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:visibility="invisible" >

            <ImageButton
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="20dip"
                android:background="@drawable/orangearrow_over"
                android:focusable="true" />

            <ImageButton
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="10dip"
                android:background="@drawable/orangearrow_over" />

            <ImageButton
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dip"
                android:background="@drawable/orangearrow_over" />
        </LinearLayout>

    </FrameLayout>

0 个答案:

没有答案