包含动态按钮的旋转框架布局 [英] Rotate Frame layout which contains dynamic buttons
问题描述
我有一个 Framelayout,它在运行时在中心添加四个图像视图,它包含用户可以执行不同操作的主图像,但我面临旋转布局视图的问题
目前我正在触摸旋转按钮
public void setRotateListener() {mRotateImage.setOnTouchListener(new OnTouchListener() {@覆盖公共布尔 onTouch(查看 v,MotionEvent 事件){浮动 x = event.getX(0);浮动 y = event.getY(0);浮动 theta = getTheta(x, y);开关 (event.getAction() & MotionEvent.ACTION_MASK) {案例 MotionEvent.ACTION_POINTER_DOWN:theta_old = theta;休息;案例 MotionEvent.ACTION_MOVE:浮动 delta_theta = theta - theta_old;theta_old = theta;int 方向 = (delta_theta > 0) ?1:-1;角度 += 3 * 方向;Log.d("Tag", "旋转角度:" + obj.getHeight());obj.setRotation(角度);通知监听器(方向);休息;}返回真;}});}私人浮动getTheta(浮动x,浮动y){浮动 sx = x - (obj.getWidth()/2.0f);float sy = y - (obj.getHeight()/2.0f);浮点长度 = (浮点数) Math.sqrt(sx * sx + sy * sy);浮动 nx = sx/长度;浮动 ny = sy/长度;float theta = (float) Math.atan2(ny, nx);最终浮点 rad2deg = (float) (180.0/Math.PI);浮动 thetaDeg = theta * rad2deg;返回 (thetaDeg <0) ?thetaDeg + 360.0f : thetaDeg;}
但我无法得到预期的结果我也已经参考了这个链接
Java 文件
import android.annotation.SuppressLint;导入 android.content.Context;导入 android.graphics.Bitmap;导入 android.graphics.Canvas;导入 android.graphics.Color;导入 android.graphics.ColorMatrixColorFilter;导入 android.graphics.Paint;导入 android.view.GestureDetector;导入 android.view.LayoutInflater;导入 android.view.MotionEvent;导入 android.view.View;导入 android.widget.ImageButton;导入 android.widget.ImageView;导入 android.widget.RelativeLayout;公共类 ClipArt 扩展了相对布局 {国际基地;int basew;int basex;国际贝西;ImageButton btndel;ImageButton btnrot;ImageButton btnscl;相对布局剪辑;上下文cntx;布尔冻结 = 假;INT H;国际我;ImageView 图像;字符串 imageUri;布尔值 isShadow;国际四;相对布局layBg;相对布局layGroup;RelativeLayout.LayoutParams layoutParams;公共 LayoutInflater mInflater;int margl;国际市场;浮动不透明度 = 1.0F;位图原位图;int pivx;int pivy;内部位置;位图 shadowBitmap;浮动起始度;字符串[] v;公共剪贴画(上下文参数上下文){超级(参数上下文);cntx = paramContext;躺组=这个;基数 = 0;基 = 0;pivx = 0;皮维 = 0;mInflater = ((LayoutInflater) paramContext.getSystemService("layout_inflater"));mInflater.inflate(R.layout.clipart, this, true);btndel = ((ImageButton) findViewById(R.id.del));btnrot = ((ImageButton) findViewById(R.id.rotate));btnscl = ((ImageButton) findViewById(R.id.sacle));layoutParams = new RelativeLayout.LayoutParams(250, 250);layGroup.setLayoutParams(layoutParams);图像 = ((ImageView) findViewById(R.id.clipart));image.setImageResource(R.drawable.ic_launcher);setOnTouchListener(new View.OnTouchListener() {最终 GestureDetectorgestureDetector = 新 GestureDetector(ClipArt.this.cntx,新 GestureDetector.SimpleOnGestureListener() {公共布尔 onDoubleTap(MotionEvent paramAnonymous2MotionEvent){返回假;}});公共布尔 onTouch(查看 paramAnonymousView,MotionEvent 事件){如果(!ClipArt.this.freeze){开关(事件.getAction()){案例 MotionEvent.ACTION_DOWN:layGroup.invalidate();手势检测器.onTouchEvent(事件);layGroup.performClick();basex = ((int) (event.getRawX() - layoutParams.leftMargin));basey = ((int) (event.getRawY() - layoutParams.topMargin));休息;案例 MotionEvent.ACTION_MOVE:int i = (int) event.getRawX();int j = (int) event.getRawY();layBg = ((RelativeLayout) getParent());if ((i - basex > -(layGroup.getWidth() * 2/3))&&(i - basex <layBg.getWidth() -layGroup.getWidth()/3)) {layoutParams.leftMargin = (i - basex);}if ((j - basey > -(layGroup.getHeight() * 2/3))&&(j - basey <layBg.getHeight() -layGroup.getHeight()/3)) {layoutParams.topMargin = (j - basey);}layoutParams.rightMargin = -1000;layoutParams.bottomMargin = -1000;layGroup.setLayoutParams(layoutParams);休息;}返回真;}返回真;}});this.btnscl.setOnTouchListener(new View.OnTouchListener() {@SuppressLint({ "NewApi" })公共布尔 onTouch(查看 paramAnonymousView,MotionEvent 事件){如果(!ClipArt.this.freeze){int j = (int) event.getRawX();int i = (int) event.getRawY();layoutParams = (RelativeLayout.LayoutParams)layGroup.getLayoutParams();开关(事件.getAction()){案例 MotionEvent.ACTION_DOWN:ClipArt.this.layGroup.invalidate();ClipArt.this.basex = j;ClipArt.this.basey = i;ClipArt.this.basew = ClipArt.this.layGroup.getWidth();ClipArt.this.baseh = ClipArt.this.layGroup.getHeight();int[] 位置 = 新的 int[2];layGroup.getLocationOnScreen(loaction);margl = layoutParams.leftMargin;margt = layoutParams.topMargin;休息;案例 MotionEvent.ACTION_MOVE:float f2 = (float) Math.toDegrees(Math.atan2(i - ClipArt.this.basey, j - ClipArt.this.basex));浮动 f1 = f2;如果 (f2 <0.0F) {f1 = f2 + 360.0F;}j -= ClipArt.this.basex;int k = i - ClipArt.this.basey;i = (int) (Math.sqrt(j * j + k * k)* Math.cos(Math.toRadians(f1 - ClipArt.this.layGroup.getRotation())));j = (int) (Math.sqrt(i * i + k * k)* Math.sin(Math.toRadians(f1 - ClipArt.this.layGroup.getRotation())));k = i * 2 + ClipArt.this.basew;int m = j * 2 + ClipArt.this.baseh;如果 (k > 150) {layoutParams.width = k;layoutParams.leftMargin = (ClipArt.this.margl - i);}如果 (m > 150) {layoutParams.height = m;layoutParams.topMargin = (ClipArt.this.margt - j);}ClipArt.this.layGroup.setLayoutParams(layoutParams);ClipArt.this.layGroup.performLongClick();休息;}返回真;}返回 ClipArt.this.freeze;}});this.btnrot.setOnTouchListener(new View.OnTouchListener() {@SuppressLint({ "NewApi" })公共布尔 onTouch(查看 paramAnonymousView,MotionEvent 事件){如果(!ClipArt.this.freeze){layoutParams = (RelativeLayout.LayoutParams) ClipArt.this.layGroup.getLayoutParams();ClipArt.this.layBg = ((RelativeLayout) ClipArt.this.getParent());int[] arrayOfInt = new int[2];layBg.getLocationOnScreen(arrayOfInt);int i = (int) event.getRawX() - arrayOfInt[0];int j = (int) event.getRawY() - arrayOfInt[1];开关(事件.getAction()){案例 MotionEvent.ACTION_DOWN:ClipArt.this.layGroup.invalidate();ClipArt.this.startDegree =layGroup.getRotation();ClipArt.this.pivx = (layoutParams.leftMargin + ClipArt.this.getWidth()/2);ClipArt.this.pivy = (layoutParams.topMargin + ClipArt.this.getHeight()/2);ClipArt.this.basex = (i - ClipArt.this.pivx);ClipArt.this.basey = (ClipArt.this.pivy - j);休息;案例 MotionEvent.ACTION_MOVE:int k = ClipArt.this.pivx;int m = ClipArt.this.pivy;j = (int) (Math.toDegrees(Math.atan2(ClipArt.this.basey, ClipArt.this.basex))- Math.toDegrees(Math.atan2(m - j, i - k)));我 = j;如果 (j <0) {i = j + 360;}ClipArt.this.layGroup.setRotation((ClipArt.this.startDegree + i) % 360.0F);休息;}返回真;}返回 ClipArt.this.freeze;}});this.btndel.setOnClickListener(new View.OnClickListener() {公共无效onClick(查看paramAnonymousView){如果(!ClipArt.this.freeze){layBg = ((RelativeLayout) ClipArt.this.getParent());layBg.performClick();layBg.removeView(ClipArt.this.layGroup);}}});}公共无效禁用所有(){this.btndel.setVisibility(4);this.btnrot.setVisibility(4);this.btnscl.setVisibility(4);}公共 ImageView getImageView() {返回 this.image;}public void setFreeze(boolean paramBoolean) {this.freeze = paramBoolean;}}
布局文件
<ImageButton android:id="@+id/rotate";android:layout_width=50dp"android:layout_height=50dp"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"android:adjustViewBounds="true"android:background="@android:color/transparent";android:scaleType="fitCenter"android:src=@drawable/rotation"/><ImageButton android:id="@+id/sacle";android:layout_width=50dp"android:layout_height=50dp"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:adjustViewBounds="true"android:background="@android:color/transparent";android:scaleType="fitCenter"android:src=@drawable/pointer"/><ImageButton android:id="@+id/del";android:layout_width=50dp"android:layout_height=50dp"android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:adjustViewBounds="true"android:background="@android:color/transparent";android:scaleType="fitCenter"android:src=@drawable/close"/><ImageView android:id="@+id/clipart";android:layout_width=match_parent"android:layout_height=match_parent"android:padding="10dp"/></RelativeLayout>
和放入 drawable 中的图像
I have a Framelayout which add four imageview at runtime as well in center it contains main image with which user can perform different action but i face the problem with rotate layout view
currently on touch of rotate button i'm doing this
public void setRotateListener() {
mRotateImage.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX(0);
float y = event.getY(0);
float theta = getTheta(x, y);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
theta_old = theta;
break;
case MotionEvent.ACTION_MOVE:
float delta_theta = theta - theta_old;
theta_old = theta;
int direction = (delta_theta > 0) ? 1 : -1;
angle += 3 * direction;
Log.d("Tag", "rotate angle : " + obj.getHeight());
obj.setRotation(angle);
notifyListener(direction);
break;
}
return true;
}
});
}
private float getTheta(float x, float y) {
float sx = x - (obj.getWidth() / 2.0f);
float sy = y - (obj.getHeight() / 2.0f);
float length = (float) Math.sqrt(sx * sx + sy * sy);
float nx = sx / length;
float ny = sy / length;
float theta = (float) Math.atan2(ny, nx);
final float rad2deg = (float) (180.0 / Math.PI);
float thetaDeg = theta * rad2deg;
return (thetaDeg < 0) ? thetaDeg + 360.0f : thetaDeg;
}
but i can't get the expected result i already refer this link as well https://github.com/rprouse/XkcdClock as well as try to rotate with gesture and animation too but it seems not working as per my move on the screen
I have Design A Layout that may work as your need. Download Demo here
Java File
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class ClipArt extends RelativeLayout {
int baseh;
int basew;
int basex;
int basey;
ImageButton btndel;
ImageButton btnrot;
ImageButton btnscl;
RelativeLayout clip;
Context cntx;
boolean freeze = false;
int h;
int i;
ImageView image;
String imageUri;
boolean isShadow;
int iv;
RelativeLayout layBg;
RelativeLayout layGroup;
RelativeLayout.LayoutParams layoutParams;
public LayoutInflater mInflater;
int margl;
int margt;
float opacity = 1.0F;
Bitmap originalBitmap;
int pivx;
int pivy;
int pos;
Bitmap shadowBitmap;
float startDegree;
String[] v;
public ClipArt(Context paramContext) {
super(paramContext);
cntx = paramContext;
layGroup = this;
basex = 0;
basey = 0;
pivx = 0;
pivy = 0;
mInflater = ((LayoutInflater) paramContext.getSystemService("layout_inflater"));
mInflater.inflate(R.layout.clipart, this, true);
btndel = ((ImageButton) findViewById(R.id.del));
btnrot = ((ImageButton) findViewById(R.id.rotate));
btnscl = ((ImageButton) findViewById(R.id.sacle));
layoutParams = new RelativeLayout.LayoutParams(250, 250);
layGroup.setLayoutParams(layoutParams);
image = ((ImageView) findViewById(R.id.clipart));
image.setImageResource(R.drawable.ic_launcher);
setOnTouchListener(new View.OnTouchListener() {
final GestureDetector gestureDetector = new GestureDetector(ClipArt.this.cntx,
new GestureDetector.SimpleOnGestureListener() {
public boolean onDoubleTap(MotionEvent paramAnonymous2MotionEvent) {
return false;
}
});
public boolean onTouch(View paramAnonymousView, MotionEvent event) {
if (!ClipArt.this.freeze) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
layGroup.invalidate();
gestureDetector.onTouchEvent(event);
layGroup.performClick();
basex = ((int) (event.getRawX() - layoutParams.leftMargin));
basey = ((int) (event.getRawY() - layoutParams.topMargin));
break;
case MotionEvent.ACTION_MOVE:
int i = (int) event.getRawX();
int j = (int) event.getRawY();
layBg = ((RelativeLayout) getParent());
if ((i - basex > -(layGroup.getWidth() * 2 / 3))
&& (i - basex < layBg.getWidth() - layGroup.getWidth() / 3)) {
layoutParams.leftMargin = (i - basex);
}
if ((j - basey > -(layGroup.getHeight() * 2 / 3))
&& (j - basey < layBg.getHeight() - layGroup.getHeight() / 3)) {
layoutParams.topMargin = (j - basey);
}
layoutParams.rightMargin = -1000;
layoutParams.bottomMargin = -1000;
layGroup.setLayoutParams(layoutParams);
break;
}
return true;
}
return true;
}
});
this.btnscl.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint({ "NewApi" })
public boolean onTouch(View paramAnonymousView, MotionEvent event) {
if (!ClipArt.this.freeze) {
int j = (int) event.getRawX();
int i = (int) event.getRawY();
layoutParams = (RelativeLayout.LayoutParams) layGroup.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
ClipArt.this.layGroup.invalidate();
ClipArt.this.basex = j;
ClipArt.this.basey = i;
ClipArt.this.basew = ClipArt.this.layGroup.getWidth();
ClipArt.this.baseh = ClipArt.this.layGroup.getHeight();
int[] loaction = new int[2];
layGroup.getLocationOnScreen(loaction);
margl = layoutParams.leftMargin;
margt = layoutParams.topMargin;
break;
case MotionEvent.ACTION_MOVE:
float f2 = (float) Math.toDegrees(Math.atan2(i - ClipArt.this.basey, j - ClipArt.this.basex));
float f1 = f2;
if (f2 < 0.0F) {
f1 = f2 + 360.0F;
}
j -= ClipArt.this.basex;
int k = i - ClipArt.this.basey;
i = (int) (Math.sqrt(j * j + k * k)
* Math.cos(Math.toRadians(f1 - ClipArt.this.layGroup.getRotation())));
j = (int) (Math.sqrt(i * i + k * k)
* Math.sin(Math.toRadians(f1 - ClipArt.this.layGroup.getRotation())));
k = i * 2 + ClipArt.this.basew;
int m = j * 2 + ClipArt.this.baseh;
if (k > 150) {
layoutParams.width = k;
layoutParams.leftMargin = (ClipArt.this.margl - i);
}
if (m > 150) {
layoutParams.height = m;
layoutParams.topMargin = (ClipArt.this.margt - j);
}
ClipArt.this.layGroup.setLayoutParams(layoutParams);
ClipArt.this.layGroup.performLongClick();
break;
}
return true;
}
return ClipArt.this.freeze;
}
});
this.btnrot.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint({ "NewApi" })
public boolean onTouch(View paramAnonymousView, MotionEvent event) {
if (!ClipArt.this.freeze) {
layoutParams = (RelativeLayout.LayoutParams) ClipArt.this.layGroup.getLayoutParams();
ClipArt.this.layBg = ((RelativeLayout) ClipArt.this.getParent());
int[] arrayOfInt = new int[2];
layBg.getLocationOnScreen(arrayOfInt);
int i = (int) event.getRawX() - arrayOfInt[0];
int j = (int) event.getRawY() - arrayOfInt[1];
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
ClipArt.this.layGroup.invalidate();
ClipArt.this.startDegree = layGroup.getRotation();
ClipArt.this.pivx = (layoutParams.leftMargin + ClipArt.this.getWidth() / 2);
ClipArt.this.pivy = (layoutParams.topMargin + ClipArt.this.getHeight() / 2);
ClipArt.this.basex = (i - ClipArt.this.pivx);
ClipArt.this.basey = (ClipArt.this.pivy - j);
break;
case MotionEvent.ACTION_MOVE:
int k = ClipArt.this.pivx;
int m = ClipArt.this.pivy;
j = (int) (Math.toDegrees(Math.atan2(ClipArt.this.basey, ClipArt.this.basex))
- Math.toDegrees(Math.atan2(m - j, i - k)));
i = j;
if (j < 0) {
i = j + 360;
}
ClipArt.this.layGroup.setRotation((ClipArt.this.startDegree + i) % 360.0F);
break;
}
return true;
}
return ClipArt.this.freeze;
}
});
this.btndel.setOnClickListener(new View.OnClickListener() {
public void onClick(View paramAnonymousView) {
if (!ClipArt.this.freeze) {
layBg = ((RelativeLayout) ClipArt.this.getParent());
layBg.performClick();
layBg.removeView(ClipArt.this.layGroup);
}
}
});
}
public void disableAll() {
this.btndel.setVisibility(4);
this.btnrot.setVisibility(4);
this.btnscl.setVisibility(4);
}
public ImageView getImageView() {
return this.image;
}
public void setFreeze(boolean paramBoolean) {
this.freeze = paramBoolean;
}
}
Layout file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<ImageButton android:id="@+id/rotate" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:adjustViewBounds="true" android:background="@android:color/transparent" android:scaleType="fitCenter" android:src="@drawable/rotation"/>
<ImageButton android:id="@+id/sacle" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:adjustViewBounds="true" android:background="@android:color/transparent" android:scaleType="fitCenter" android:src="@drawable/pointer"/>
<ImageButton android:id="@+id/del" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:adjustViewBounds="true" android:background="@android:color/transparent" android:scaleType="fitCenter" android:src="@drawable/close"/>
<ImageView android:id="@+id/clipart" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"/>
</RelativeLayout>
and images put in drawable
这篇关于包含动态按钮的旋转框架布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!