为什么图像在旋转后从android中的图像视图扩展视图范围

时间:2014-04-28 05:01:15

标签: android image rotation android-imageview ontouchlistener

我在android中拍摄了一张图片。如果我使用onTouchListener旋转图像,它的视图边界会自动扩展,并且会自动退出布局。任何人都可以帮我解决这个问题。

我已附上图片以获得更多说明。

Before rotation image looks like this

After rotation image looks like this

图像视图和旋转代码如下所示。

XML Layout ::

<RelativeLayout
    android:id="@+id/rlUserProfileLayout"
    android:layout_width="fill_parent"
    android:layout_height="@dimen/browseUserImageSize"
    android:layout_below="@id/rlServiceNameLayout"
    android:layout_marginLeft="@dimen/browseLeftRightMargin"
    android:layout_marginRight="@dimen/browseLeftRightMargin"
    android:layout_marginTop="@dimen/browseVIPImageTopMargin"
    android:background="@drawable/user_profile_background_color">

    <ImageView
        android:id="@+id/imgUserProfile"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/browseUserImageSize"
        android:contentDescription="@string/app_name"
        android:scaleType="matrix"
        android:src="@drawable/user_image"
         >
    </ImageView>

    <ImageView
        android:id="@+id/btnLike"
        android:layout_width="@dimen/browseLikeButtonHeightWidth"
        android:layout_height="@dimen/browseLikeButtonHeightWidth"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:src="@drawable/button_like_up_big"
        android:alpha="0" />

    <ImageView
        android:id="@+id/btnDisLike"
        android:layout_width="@dimen/browseLikeButtonHeightWidth"
        android:layout_height="@dimen/browseLikeButtonHeightWidth"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/button_like_down_big"
        android:alpha="0" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="@dimen/browseUserPicDetailLayoutHeight"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@drawable/user_profile_detail_bottom_layout" >

        <com.companyname.servicebarter.CustomTextView
            android:id="@+id/txtSharedContacts"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="@dimen/browseUserPicDetailMargin"
            android:text="@string/browseSharedContact"
            android:textColor="@color/fc_white"
            android:textSize="@dimen/browseSharedContactTextSize"
            android:textStyle="bold" />

        <com.companyname.servicebarter.CustomTextView
            android:id="@+id/txtDistance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="@dimen/browseUserPicDetailMargin"
            android:text="@string/browseDistance"
            android:textColor="@color/fc_white"
            android:textSize="@dimen/browseSharedContactTextSize" />

        <LinearLayout
            android:id="@+id/layoutRating1"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/browseUserPicDetailLayoutHeight"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:gravity="center_vertical"
            android:orientation="horizontal" >
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

Java code ::

relativeLayout.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            homeActivity.getPager().setPaging(false);
            switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                start.set(event.getRawX(), event.getRawY());

                break;
            case MotionEvent.ACTION_MOVE:

                v.setTranslationX(event.getRawX() - start.x);
                v.setTranslationY(event.getRawY() - start.y);
                x_cord = (int) ((int) event.getRawX() - start.x);
                y_cord = (int) ((int) event.getRawY() - start.y);
                v.setRotation((float) ((x_cord) * (Math.PI / 32)));

                if (x_cord >= 0) {

                    if (x_cord > (screenCenter / 2)) {
                        // imageLike.setAlpha(1);
                        /*
                         * SET ALPHA BY MATYHEMATIC RULE Y=MX+C THIS FORMULA
                         * GIVE US LINEAR VALUE
                         */
                        btnLike.setAlpha((float) (event.getRawX() / (2 * (screenCenter))));
                        // btnLike.setVisibility(View.VISIBLE);
                        Log.d("ads",
                                ""
                                        + (float) (event.getRawX() / (0.5 * (screenCenter))));
                        if (x_cord > (screenCenter / 4)) {
                            Likes = 2;
                        } else {
                            Likes = 0;
                        }
                    } else {
                        Likes = 0;
                        btnLike.setAlpha(0.0f);

                    }
                    btnDisLike.setAlpha(0.0f);
                } else {
                    // rotate

                    if (Math.abs(x_cord) > (screenCenter / 2)) {

                        btnDisLike.setAlpha(1 - (float) (event.getRawX() / (0.5 * screenCenter)));

                        if (x_cord < screenCenter / 4) {
                            Likes = 1;
                        } else {
                            Likes = 0;
                        }
                    } else {
                        Likes = 0;
                        btnDisLike.setAlpha(0.0f);
                    }
                    // imageLike.setAlpha(0);
                    // btnLike.setVisibility(View.GONE);
                    btnLike.setAlpha(0.0f);
                }

                break;
            case MotionEvent.ACTION_UP:
                btnLike.setAlpha(0.0f);
                btnDisLike.setAlpha(0.0f);

                homeActivity.getPager().setPaging(true);
                if (Likes == 0) {
                    v.animate().translationX(_xDelta);
                    v.animate().translationY(_yDelta);
                    v.setRotation(0.0f);
                } else if (Likes == 1) {
                    v.animate().translationX(_xDelta);
                    v.animate().translationY(_yDelta);
                    v.setRotation(0.0f);

                    buttonDisLikeClicked();

                } else if (Likes == 2) {

                    v.animate().translationX(_xDelta);
                    v.animate().translationY(_yDelta);
                    v.setRotation(0.0f);
                    buttonLikeClicked();

                }
                break;

            }
            return true;
        }
    });

任何人都可以建议我为什么要面对这个问题?提前谢谢。

0 个答案:

没有答案