Java 如何在RecyclerView中旋转项目视图?
我正在构建一个应用程序,它有一个在横向模式下固定的活动。当用户旋转屏幕时,它不会破坏和重新创建活动,而是旋转视图并停留在风景中 我通过扩展RotateAnimation类为所有其他视图设置了动画,但我不知道如何在RecyclerView中旋转图标。我试图将adapter类中的ViewHolder传递给animation类,但没有成功 这是我的密码 MainActivity.javaJava 如何在RecyclerView中旋转项目视图?,java,android,animation,android-recyclerview,Java,Android,Animation,Android Recyclerview,我正在构建一个应用程序,它有一个在横向模式下固定的活动。当用户旋转屏幕时,它不会破坏和重新创建活动,而是旋转视图并停留在风景中 我通过扩展RotateAnimation类为所有其他视图设置了动画,但我不知道如何在RecyclerView中旋转图标。我试图将adapter类中的ViewHolder传递给animation类,但没有成功 这是我的密码 MainActivity.java private void prepOrientationHandler(){ orientatio
private void prepOrientationHandler(){
orientationHandler = new OrientationHandler(this, getRotation());
orientationHandler.setViews(myIcon);
}
public class ViewRotationAnim extends RotateAnimation {
float mFrom;
float mTo;
float mPivotX;
float mPivotY;
float mPivotXValue;
float mPivotYValue;
public ViewRotationAnim(float mFrom, float mTo, float mPivotX, float mPivotY) {
super(mFrom, mTo, Animation.RELATIVE_TO_SELF, mPivotX, Animation.RELATIVE_TO_SELF, mPivotY);
this.mFrom = mFrom;
this.mTo = mTo;
this.mPivotX = mPivotX;
this.mPivotY = mPivotY;
this.mPivotXValue = mPivotX;
this.mPivotYValue = mPivotY;
}
public void setFromTo(float mFrom, float mTo) {
this.mFrom = mFrom;
this.mTo = mTo;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mPivotX = resolveSize(Animation.RELATIVE_TO_SELF, mPivotXValue, width, parentWidth);
mPivotY = resolveSize(Animation.RELATIVE_TO_SELF, mPivotYValue, height, parentHeight);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float degrees = 0;
if (mFrom == 0 && mTo == 270) {
degrees = 360 - (90 * interpolatedTime);
} else if (mFrom == 270 && mTo == 0) {
degrees = 270 + (90 * interpolatedTime);
} else {
degrees = mFrom + ((mTo - mFrom) * interpolatedTime);
}
float scale = getScaleFactor();
if (mPivotX == 0.0f && mPivotY == 0.0f) {
t.getMatrix().setRotate(degrees);
} else {
t.getMatrix().setRotate(degrees, mPivotX * scale, mPivotY * scale);
}
}
public class OrientationHandler extends OrientationEventListener {
final int ROTATION_0 = 0;
final int ROTATION_90 = 90;
final int ROTATION_180 = 180;
final int ROTATION_270 = 270;
private int rotation;
private int currentViewRotation = 0;
private ImageView myIcon;
private ViewRotationAnim rotationAnimForIcons;
public OrientationHandler(Context context, int rotation) {
super(context);
rotationAnimForIcons = new ViewRotationAnim(0, 0, 0.5f, 0.5f);
rotationAnimForIcons.setDuration(500);
rotationAnimForIcons.setFillAfter(true);
this.rotation = rotation;
}
public void setViews(View... views) {
myIcon = (ImageView) views[0];
}
private void rotateView() {
myIcon.startAnimation(rotationAnimForIcons);
}
@Override
public void onOrientationChanged(int orientation) {
if ((orientation < 35 || orientation > 325) && rotation != ROTATION_0) {
rotationAnimForIcons.setFromTo(currentViewRotation, ROTATION_270);
rotateViews();
rotation = ROTATION_0;
currentViewRotation = ROTATION_270;
} else if (orientation > 145 && orientation < 215 && rotation != ROTATION_180) {
rotationAnimForIcons.setFromTo(currentViewRotation, ROTATION_90);
rotateViews();
rotation = ROTATION_180;
currentViewRotation = ROTATION_90;
} else if (orientation > 55 && orientation < 125 && rotation != ROTATION_270) {
rotationAnimForIcons.setFromTo(currentViewRotation, ROTATION_180);
rotateViews();
rotation = ROTATION_270;
currentViewRotation = ROTATION_180;
} else if (orientation > 235 && orientation < 305 && rotation != ROTATION_90) {
rotationAnimForIcons.setFromTo(currentViewRotation, ROTATION_0);
rotateViews();
rotation = ROTATION_90;
currentViewRotation = ROTATION_0;
}
}
public class IconListAdapter extends RecyclerView.Adapter<IconListAdapter.ViewHolder> {
private Context mContext;
private RealmResults<MyIcon> mIcons;
public IconListAdapter(Context mContext, RealmResults<MyIcon> mIcons) {
this.mContext = mContext;
this.mIcons = mIcons;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView mIconView;
public ViewHolder(View itemView) {
super(itemView);
this.mIconView = (ImageView) itemView.findViewById(R.id.main_activity_list_item);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.video_sound_list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
MyIcon currentIcon = mIcons.get(position);
holder.mIconView.setImageResource(currentIcon.getIconResource());
}
@Override
public int getItemCount() {
return mIcons.size();
}
}
ViewRotationAnim.java
private void prepOrientationHandler(){
orientationHandler = new OrientationHandler(this, getRotation());
orientationHandler.setViews(myIcon);
}
public class ViewRotationAnim extends RotateAnimation {
float mFrom;
float mTo;
float mPivotX;
float mPivotY;
float mPivotXValue;
float mPivotYValue;
public ViewRotationAnim(float mFrom, float mTo, float mPivotX, float mPivotY) {
super(mFrom, mTo, Animation.RELATIVE_TO_SELF, mPivotX, Animation.RELATIVE_TO_SELF, mPivotY);
this.mFrom = mFrom;
this.mTo = mTo;
this.mPivotX = mPivotX;
this.mPivotY = mPivotY;
this.mPivotXValue = mPivotX;
this.mPivotYValue = mPivotY;
}
public void setFromTo(float mFrom, float mTo) {
this.mFrom = mFrom;
this.mTo = mTo;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mPivotX = resolveSize(Animation.RELATIVE_TO_SELF, mPivotXValue, width, parentWidth);
mPivotY = resolveSize(Animation.RELATIVE_TO_SELF, mPivotYValue, height, parentHeight);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float degrees = 0;
if (mFrom == 0 && mTo == 270) {
degrees = 360 - (90 * interpolatedTime);
} else if (mFrom == 270 && mTo == 0) {
degrees = 270 + (90 * interpolatedTime);
} else {
degrees = mFrom + ((mTo - mFrom) * interpolatedTime);
}
float scale = getScaleFactor();
if (mPivotX == 0.0f && mPivotY == 0.0f) {
t.getMatrix().setRotate(degrees);
} else {
t.getMatrix().setRotate(degrees, mPivotX * scale, mPivotY * scale);
}
}
public class OrientationHandler extends OrientationEventListener {
final int ROTATION_0 = 0;
final int ROTATION_90 = 90;
final int ROTATION_180 = 180;
final int ROTATION_270 = 270;
private int rotation;
private int currentViewRotation = 0;
private ImageView myIcon;
private ViewRotationAnim rotationAnimForIcons;
public OrientationHandler(Context context, int rotation) {
super(context);
rotationAnimForIcons = new ViewRotationAnim(0, 0, 0.5f, 0.5f);
rotationAnimForIcons.setDuration(500);
rotationAnimForIcons.setFillAfter(true);
this.rotation = rotation;
}
public void setViews(View... views) {
myIcon = (ImageView) views[0];
}
private void rotateView() {
myIcon.startAnimation(rotationAnimForIcons);
}
@Override
public void onOrientationChanged(int orientation) {
if ((orientation < 35 || orientation > 325) && rotation != ROTATION_0) {
rotationAnimForIcons.setFromTo(currentViewRotation, ROTATION_270);
rotateViews();
rotation = ROTATION_0;
currentViewRotation = ROTATION_270;
} else if (orientation > 145 && orientation < 215 && rotation != ROTATION_180) {
rotationAnimForIcons.setFromTo(currentViewRotation, ROTATION_90);
rotateViews();
rotation = ROTATION_180;
currentViewRotation = ROTATION_90;
} else if (orientation > 55 && orientation < 125 && rotation != ROTATION_270) {
rotationAnimForIcons.setFromTo(currentViewRotation, ROTATION_180);
rotateViews();
rotation = ROTATION_270;
currentViewRotation = ROTATION_180;
} else if (orientation > 235 && orientation < 305 && rotation != ROTATION_90) {
rotationAnimForIcons.setFromTo(currentViewRotation, ROTATION_0);
rotateViews();
rotation = ROTATION_90;
currentViewRotation = ROTATION_0;
}
}
public class IconListAdapter extends RecyclerView.Adapter<IconListAdapter.ViewHolder> {
private Context mContext;
private RealmResults<MyIcon> mIcons;
public IconListAdapter(Context mContext, RealmResults<MyIcon> mIcons) {
this.mContext = mContext;
this.mIcons = mIcons;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView mIconView;
public ViewHolder(View itemView) {
super(itemView);
this.mIconView = (ImageView) itemView.findViewById(R.id.main_activity_list_item);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.video_sound_list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
MyIcon currentIcon = mIcons.get(position);
holder.mIconView.setImageResource(currentIcon.getIconResource());
}
@Override
public int getItemCount() {
return mIcons.size();
}
}
}
OrientationHandler.java
private void prepOrientationHandler(){
orientationHandler = new OrientationHandler(this, getRotation());
orientationHandler.setViews(myIcon);
}
public class ViewRotationAnim extends RotateAnimation {
float mFrom;
float mTo;
float mPivotX;
float mPivotY;
float mPivotXValue;
float mPivotYValue;
public ViewRotationAnim(float mFrom, float mTo, float mPivotX, float mPivotY) {
super(mFrom, mTo, Animation.RELATIVE_TO_SELF, mPivotX, Animation.RELATIVE_TO_SELF, mPivotY);
this.mFrom = mFrom;
this.mTo = mTo;
this.mPivotX = mPivotX;
this.mPivotY = mPivotY;
this.mPivotXValue = mPivotX;
this.mPivotYValue = mPivotY;
}
public void setFromTo(float mFrom, float mTo) {
this.mFrom = mFrom;
this.mTo = mTo;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mPivotX = resolveSize(Animation.RELATIVE_TO_SELF, mPivotXValue, width, parentWidth);
mPivotY = resolveSize(Animation.RELATIVE_TO_SELF, mPivotYValue, height, parentHeight);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float degrees = 0;
if (mFrom == 0 && mTo == 270) {
degrees = 360 - (90 * interpolatedTime);
} else if (mFrom == 270 && mTo == 0) {
degrees = 270 + (90 * interpolatedTime);
} else {
degrees = mFrom + ((mTo - mFrom) * interpolatedTime);
}
float scale = getScaleFactor();
if (mPivotX == 0.0f && mPivotY == 0.0f) {
t.getMatrix().setRotate(degrees);
} else {
t.getMatrix().setRotate(degrees, mPivotX * scale, mPivotY * scale);
}
}
public class OrientationHandler extends OrientationEventListener {
final int ROTATION_0 = 0;
final int ROTATION_90 = 90;
final int ROTATION_180 = 180;
final int ROTATION_270 = 270;
private int rotation;
private int currentViewRotation = 0;
private ImageView myIcon;
private ViewRotationAnim rotationAnimForIcons;
public OrientationHandler(Context context, int rotation) {
super(context);
rotationAnimForIcons = new ViewRotationAnim(0, 0, 0.5f, 0.5f);
rotationAnimForIcons.setDuration(500);
rotationAnimForIcons.setFillAfter(true);
this.rotation = rotation;
}
public void setViews(View... views) {
myIcon = (ImageView) views[0];
}
private void rotateView() {
myIcon.startAnimation(rotationAnimForIcons);
}
@Override
public void onOrientationChanged(int orientation) {
if ((orientation < 35 || orientation > 325) && rotation != ROTATION_0) {
rotationAnimForIcons.setFromTo(currentViewRotation, ROTATION_270);
rotateViews();
rotation = ROTATION_0;
currentViewRotation = ROTATION_270;
} else if (orientation > 145 && orientation < 215 && rotation != ROTATION_180) {
rotationAnimForIcons.setFromTo(currentViewRotation, ROTATION_90);
rotateViews();
rotation = ROTATION_180;
currentViewRotation = ROTATION_90;
} else if (orientation > 55 && orientation < 125 && rotation != ROTATION_270) {
rotationAnimForIcons.setFromTo(currentViewRotation, ROTATION_180);
rotateViews();
rotation = ROTATION_270;
currentViewRotation = ROTATION_180;
} else if (orientation > 235 && orientation < 305 && rotation != ROTATION_90) {
rotationAnimForIcons.setFromTo(currentViewRotation, ROTATION_0);
rotateViews();
rotation = ROTATION_90;
currentViewRotation = ROTATION_0;
}
}
public class IconListAdapter extends RecyclerView.Adapter<IconListAdapter.ViewHolder> {
private Context mContext;
private RealmResults<MyIcon> mIcons;
public IconListAdapter(Context mContext, RealmResults<MyIcon> mIcons) {
this.mContext = mContext;
this.mIcons = mIcons;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView mIconView;
public ViewHolder(View itemView) {
super(itemView);
this.mIconView = (ImageView) itemView.findViewById(R.id.main_activity_list_item);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.video_sound_list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
MyIcon currentIcon = mIcons.get(position);
holder.mIconView.setImageResource(currentIcon.getIconResource());
}
@Override
public int getItemCount() {
return mIcons.size();
}
}
public类OrientationHandler扩展了OrientationEventListener{
最终整数旋转_0=0;
最终整数旋转_90=90;
最终整数旋转_180=180;
最终整数旋转_270=270;
私有整数旋转;
private int currentViewRotation=0;
私有图像视图myIcon;
私有视图旋转anim旋转anim图标;
公共方向处理程序(上下文,int旋转){
超级(上下文);
rotationAnimForIcons=新视图rotationanim(0,0,0.5f,0.5f);
图标的旋转时间。设置持续时间(500);
rotationAnimForIcons.setFillAfter(真);
这个旋转=旋转;
}
公共无效集合视图(视图…视图){
myIcon=(ImageView)视图[0];
}
私有void rotateView(){
myIcon.startAnimation(图标的旋转);
}
@凌驾
公共无效onOrientationChanged(int方向){
如果((方向<35 | |方向>325)和旋转!=旋转0){
rotationAnimForIcons.setFromTo(当前视图旋转、旋转);
旋转视图();
旋转=旋转0;
currentViewRotation=ROTATION_270;
}否则如果(方向>145&&orientation<215&&rotation!=旋转180){
rotationAnimForIcons.setFromTo(当前视图旋转,旋转90);
旋转视图();
旋转=旋转180度;
currentViewRotation=ROTATION_90;
}否则如果(方向>55和方向<125和旋转!=旋转270){
rotationAnimForIcons.setFromTo(当前视图旋转,旋转180);
旋转视图();
旋转=旋转_270;
currentViewRotation=ROTATION_180;
}否则如果(方向>235&&方向<305&&旋转!=旋转90){
rotationAnimForIcons.setFromTo(currentViewRotation,ROTATION_0);
旋转视图();
旋转=旋转90度;
currentViewRotation=ROTATION_0;
}
}
}
iconlistapter.java
private void prepOrientationHandler(){
orientationHandler = new OrientationHandler(this, getRotation());
orientationHandler.setViews(myIcon);
}
public class ViewRotationAnim extends RotateAnimation {
float mFrom;
float mTo;
float mPivotX;
float mPivotY;
float mPivotXValue;
float mPivotYValue;
public ViewRotationAnim(float mFrom, float mTo, float mPivotX, float mPivotY) {
super(mFrom, mTo, Animation.RELATIVE_TO_SELF, mPivotX, Animation.RELATIVE_TO_SELF, mPivotY);
this.mFrom = mFrom;
this.mTo = mTo;
this.mPivotX = mPivotX;
this.mPivotY = mPivotY;
this.mPivotXValue = mPivotX;
this.mPivotYValue = mPivotY;
}
public void setFromTo(float mFrom, float mTo) {
this.mFrom = mFrom;
this.mTo = mTo;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mPivotX = resolveSize(Animation.RELATIVE_TO_SELF, mPivotXValue, width, parentWidth);
mPivotY = resolveSize(Animation.RELATIVE_TO_SELF, mPivotYValue, height, parentHeight);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float degrees = 0;
if (mFrom == 0 && mTo == 270) {
degrees = 360 - (90 * interpolatedTime);
} else if (mFrom == 270 && mTo == 0) {
degrees = 270 + (90 * interpolatedTime);
} else {
degrees = mFrom + ((mTo - mFrom) * interpolatedTime);
}
float scale = getScaleFactor();
if (mPivotX == 0.0f && mPivotY == 0.0f) {
t.getMatrix().setRotate(degrees);
} else {
t.getMatrix().setRotate(degrees, mPivotX * scale, mPivotY * scale);
}
}
public class OrientationHandler extends OrientationEventListener {
final int ROTATION_0 = 0;
final int ROTATION_90 = 90;
final int ROTATION_180 = 180;
final int ROTATION_270 = 270;
private int rotation;
private int currentViewRotation = 0;
private ImageView myIcon;
private ViewRotationAnim rotationAnimForIcons;
public OrientationHandler(Context context, int rotation) {
super(context);
rotationAnimForIcons = new ViewRotationAnim(0, 0, 0.5f, 0.5f);
rotationAnimForIcons.setDuration(500);
rotationAnimForIcons.setFillAfter(true);
this.rotation = rotation;
}
public void setViews(View... views) {
myIcon = (ImageView) views[0];
}
private void rotateView() {
myIcon.startAnimation(rotationAnimForIcons);
}
@Override
public void onOrientationChanged(int orientation) {
if ((orientation < 35 || orientation > 325) && rotation != ROTATION_0) {
rotationAnimForIcons.setFromTo(currentViewRotation, ROTATION_270);
rotateViews();
rotation = ROTATION_0;
currentViewRotation = ROTATION_270;
} else if (orientation > 145 && orientation < 215 && rotation != ROTATION_180) {
rotationAnimForIcons.setFromTo(currentViewRotation, ROTATION_90);
rotateViews();
rotation = ROTATION_180;
currentViewRotation = ROTATION_90;
} else if (orientation > 55 && orientation < 125 && rotation != ROTATION_270) {
rotationAnimForIcons.setFromTo(currentViewRotation, ROTATION_180);
rotateViews();
rotation = ROTATION_270;
currentViewRotation = ROTATION_180;
} else if (orientation > 235 && orientation < 305 && rotation != ROTATION_90) {
rotationAnimForIcons.setFromTo(currentViewRotation, ROTATION_0);
rotateViews();
rotation = ROTATION_90;
currentViewRotation = ROTATION_0;
}
}
public class IconListAdapter extends RecyclerView.Adapter<IconListAdapter.ViewHolder> {
private Context mContext;
private RealmResults<MyIcon> mIcons;
public IconListAdapter(Context mContext, RealmResults<MyIcon> mIcons) {
this.mContext = mContext;
this.mIcons = mIcons;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView mIconView;
public ViewHolder(View itemView) {
super(itemView);
this.mIconView = (ImageView) itemView.findViewById(R.id.main_activity_list_item);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.video_sound_list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
MyIcon currentIcon = mIcons.get(position);
holder.mIconView.setImageResource(currentIcon.getIconResource());
}
@Override
public int getItemCount() {
return mIcons.size();
}
}
公共类IconListAdapter扩展了RecyclerView.Adapter{
私有上下文;
私有实体结果mIcons;
公共IconListAdapter(上下文mContext、RealmResults mIcons){
this.mContext=mContext;
this.mIcons=mIcons;
}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
图像视图mIconView;
公共视图持有者(视图项视图){
超级(项目视图);
this.mIconView=(ImageView)itemView.findViewById(R.id.main\u activity\u list\u item);
}
}
@凌驾
public ViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图=布局更平坦。从(mContext)。充气(R.layout.video\u sound\u list\u项,父项,false);
返回新的ViewHolder(视图);
}
@凌驾
公共无效onBindViewHolder(ViewHolder,int位置){
MyIcon currentIcon=mIcons.get(位置);
holder.mIconView.setImageResource(currentIcon.getIconResource());
}
@凌驾
public int getItemCount(){
返回mIcons.size();
}
}
此代码适用于普通视图。但我不知道从哪里开始在我的回收视图中循环项目。
有什么想法吗