- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试将 mainImg 置于屏幕底部。
/*
* Copyright (C) 2012 Capricorn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
在 ArcLayout
OnMeasure
中,如果我将大小更改为 0,它会下降但动画会消失。如何更改 OnMeasure
使 img 下降到屏幕底部,高度应该向上?请帮忙!
package com.example.splashscreen;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.Animation.AnimationListener;
/**
* A Layout that arranges its children around its center. The arc can be set by
* calling {@link #setArc(float, float) setArc()}. You can override the method
* {@link #onMeasure(int, int) onMeasure()}, otherwise it is always
* WRAP_CONTENT.
*
* @author Capricorn
*
*/
public class ArcLayout extends ViewGroup {
/**
* children will be set the same size.
*/
private int mChildSize;
private int mChildPadding = 5;
private int mLayoutPadding = 10;
public static final float DEFAULT_FROM_DEGREES = 270.0f;
public static final float DEFAULT_TO_DEGREES = 360.0f;
private float mFromDegrees = DEFAULT_FROM_DEGREES;
private float mToDegrees = DEFAULT_TO_DEGREES;
private static final int MIN_RADIUS = 150;
/* the distance between the layout's center and any child's center */
private int mRadius;
private boolean mExpanded = false;
public ArcLayout(Context context) {
super(context);
}
public ArcLayout(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ArcLayout, 0, 0);
mFromDegrees = a.getFloat(R.styleable.ArcLayout_fromDegrees, DEFAULT_FROM_DEGREES);
mToDegrees = a.getFloat(R.styleable.ArcLayout_toDegrees, DEFAULT_TO_DEGREES);
mChildSize = Math.max(a.getDimensionPixelSize(R.styleable.ArcLayout_childSize, 0), 0);
a.recycle();
}
}
private static int computeRadius(final float arcDegrees, final int childCount, final int childSize,
final int childPadding, final int minRadius) {
if (childCount < 2) {
return minRadius;
}
final float perDegrees = arcDegrees / (childCount - 1);
final float perHalfDegrees = perDegrees / 2;
final int perSize = childSize + childPadding;
final int radius = (int) ((perSize / 2) / Math.sin(Math.toRadians(perHalfDegrees)));
return Math.max(radius, minRadius);
}
private static Rect computeChildFrame(final int centerX, final int centerY, final int radius, final float degrees,
final int size) {
final double childCenterX = centerX + radius * Math.cos(Math.toRadians(degrees));
final double childCenterY = centerY + radius * Math.sin(Math.toRadians(degrees));
return new Rect((int) (childCenterX - size / 2), (int) (childCenterY),
(int) (childCenterX + size / 2), (int) (childCenterY + size ));
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int radius = mRadius = computeRadius(Math.abs(mToDegrees - mFromDegrees), getChildCount(), mChildSize,
mChildPadding, MIN_RADIUS);
final int size = radius * 2 + mChildSize + mChildPadding + mLayoutPadding * 2;
setMeasuredDimension(size, size);
final int count = getChildCount();
for (int i = 0; i < count; i++) {
getChildAt(i).measure(MeasureSpec.makeMeasureSpec(mChildSize, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(mChildSize, MeasureSpec.EXACTLY));
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int centerX = getWidth() / 2;
final int centerY = getHeight() / 2;
final int radius = mExpanded ? mRadius : 0;
final int childCount = getChildCount();
final float perDegrees = (mToDegrees - mFromDegrees) / (childCount - 1);
float degrees = mFromDegrees;
for (int i = 0; i < childCount; i++) {
Rect frame = computeChildFrame(centerX, centerY, radius, degrees, mChildSize);
degrees += perDegrees;
getChildAt(i).layout(frame.left, frame.top, frame.right, frame.bottom);
}
}
/**
* refers to {@link LayoutAnimationController#getDelayForView(View view)}
*/
private static long computeStartOffset(final int childCount, final boolean expanded, final int index,
final float delayPercent, final long duration, Interpolator interpolator) {
final float delay = delayPercent * duration;
final long viewDelay = (long) (getTransformedIndex(expanded, childCount, index) * delay);
final float totalDelay = delay * childCount;
float normalizedDelay = viewDelay / totalDelay;
normalizedDelay = interpolator.getInterpolation(normalizedDelay);
return (long) (normalizedDelay * totalDelay);
}
private static int getTransformedIndex(final boolean expanded, final int count, final int index) {
if (expanded) {
return count - 1 - index;
}
return index;
}
private static Animation createExpandAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta,
long startOffset, long duration, Interpolator interpolator) {
Animation animation = new RotateAndTranslateAnimation(0, toXDelta, 0, toYDelta, 0, 720);
animation.setStartOffset(startOffset);
animation.setDuration(duration);
animation.setInterpolator(interpolator);
animation.setFillAfter(true);
return animation;
}
private static Animation createShrinkAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta,
long startOffset, long duration, Interpolator interpolator) {
AnimationSet animationSet = new AnimationSet(false);
animationSet.setFillAfter(true);
final long preDuration = duration / 2;
Animation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setStartOffset(startOffset);
rotateAnimation.setDuration(preDuration);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setFillAfter(true);
animationSet.addAnimation(rotateAnimation);
Animation translateAnimation = new RotateAndTranslateAnimation(0, toXDelta, 0, toYDelta, 360, 720);
translateAnimation.setStartOffset(startOffset + preDuration);
translateAnimation.setDuration(duration - preDuration);
translateAnimation.setInterpolator(interpolator);
translateAnimation.setFillAfter(true);
animationSet.addAnimation(translateAnimation);
return animationSet;
}
private void bindChildAnimation(final View child, final int index, final long duration) {
final boolean expanded = mExpanded;
final int centerX = getWidth() / 2;
final int centerY = getHeight() / 2;
final int radius = expanded ? 0 : mRadius;
final int childCount = getChildCount();
final float perDegrees = (mToDegrees - mFromDegrees) / (childCount - 1);
Rect frame = computeChildFrame(centerX, centerY, radius, mFromDegrees + index * perDegrees, mChildSize);
final int toXDelta = frame.left - child.getLeft();
final int toYDelta = frame.top - child.getTop();
Interpolator interpolator = mExpanded ? new AccelerateInterpolator() : new OvershootInterpolator(1.5f);
final long startOffset = computeStartOffset(childCount, mExpanded, index, 0.1f, duration, interpolator);
Animation animation = mExpanded ? createShrinkAnimation(0, toXDelta, 0, toYDelta, startOffset, duration,
interpolator) : createExpandAnimation(0, toXDelta, 0, toYDelta, startOffset, duration, interpolator);
final boolean isLast = getTransformedIndex(expanded, childCount, index) == childCount - 1;
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (isLast) {
postDelayed(new Runnable() {
@Override
public void run() {
onAllAnimationsEnd();
}
}, 0);
}
}
});
child.setAnimation(animation);
}
public boolean isExpanded() {
return mExpanded;
}
public void setArc(float fromDegrees, float toDegrees) {
if (mFromDegrees == fromDegrees && mToDegrees == toDegrees) {
return;
}
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
requestLayout();
}
public void setChildSize(int size) {
if (mChildSize == size || size < 0) {
return;
}
mChildSize = size;
requestLayout();
}
/**
* switch between expansion and shrinkage
*
* @param showAnimation
*/
public void switchState(final boolean showAnimation) {
if (showAnimation) {
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
bindChildAnimation(getChildAt(i), i, 300);
}
}
mExpanded = !mExpanded;
if (!showAnimation) {
requestLayout();
}
invalidate();
}
private void onAllAnimationsEnd() {
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
getChildAt(i).clearAnimation();
}
requestLayout();
}
}
最佳答案
正如您在下面的 daCapricorn comment 中看到的那样在类似的问题上报道。
他详细介绍了可能的解决方案:
在 xml 中设置 android:layout_alignParentBottom="true"和 android:layout_alignParentLeft="true"并以编程方式计算 marginBottom 和 marginLeft(它们应该是负数)。 marginBottom = (ArcMenu 的高度 centerIcon 的高度)/2 , marginLeft = (ArcMenu 的宽度 - centerIcon 的宽度)/2。
这里的主要思想是将所需的边距设置为负值(我的例子是右下角)。我设法通过计算所需的边距来解决这个问题 marginBottom = - ((ArcMenu 的高度 centerIcon 的高度)/2 ), marginRight = -((ArcMenu 的宽度 - centerIcon 的宽度)/2).
我没有在库代码的 ArcLayout.java 的 onMeasure 内部执行此操作,我在 Activity 中执行了以下操作。
在我的 Activity 布局中根据需要设置 layout_alignParent :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:arc="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.capricorn.ArcMenu
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:id="@+id/arc_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
arc:childSize="@dimen/menuChildSize"
arc:fromDegrees="180"
arc:toDegrees="270" />
</RelativeLayout>
在我的 Activity 中:
ArcMenu arcMenu; // the arc menu
FrameLayout controlLayout; // the frame layout of the control from library
int arcWidth, arcHeight; // arc menu measurments to be calculated , required for margins
int controlLayoutWidth, controlLayoutHeight; // controlLayout measurments to be calculated , required for margins
boolean isArcMeasureReady, isControlLayoutMeasureReady; // determine if measurements are ready if so apply the margins with the following runnable
Runnable runnable = new Runnable() {
@Override
public void run() {
if (isArcMeasureReady && isControlLayoutMeasureReady) {
RelativeLayout.LayoutParams params = (LayoutParams) arcMenu // your container LayoutParmas , mine is RelativeLayout
.getLayoutParams();
int rightMargin = -((arcWidth - crossWidth) / 2);
int bottomMargin = -((arcHeight - crossHeight) / 2);
params.setMargins(0, 0, rightMargin, bottomMargin);
arcMenu.setLayoutParams(params);
}
}
};
我的 onCreate :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
isArcMeasureReady = isControlLayoutMeasureReady = false;
controlLayout = (FrameLayout) findViewById(com.capricorn.R.id.control_layout);
...
ViewTreeObserver viewTreeObserverControlLayout = controlLayout.getViewTreeObserver();
if (viewTreeObserverControlLayout.isAlive()) {
viewTreeObserverControlLayout
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
controlLayout.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
controlLayoutWidth = controlLayout.getWidth();
controlLayoutHeight = controlLayout.getHeight();
isControlLayoutMeasureReady = true;
runOnUiThread(runnable);
}
});
}
ViewTreeObserver viewTreeObserver = arcMenu.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
arcMenu.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
arcWidth = arcMenu.getWidth();
arcHeight = arcMenu.getHeight();
isArcMeasureReady = true;
runOnUiThread(runnable);
}
});
}
我在多台设备上对此进行了测试,效果很好,因为计算是动态的。
希望这有助于享受这个人的精彩小部件 daCapricorn
关于java - ArcMenu mainImage 我需要在屏幕底部。 OnMeasure 不让它出现在屏幕底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13412908/
我做了一个自定义 View 。如果我将 View 添加到布局 XML 文件并将高度设置为 fill_parent“specSize”返回 0。为什么? 代码: @Override protec
我目前正在开发一个相机应用程序,我必须根据(全屏)应用程序 View 的大小设置相机预览大小。该过程涉及等待 onMeasure 事件获取 View 的大小,并根据其他几个参数(填充、相机图片大小等)
我在 recyclerView 中使用了相当复杂的 recyclerView 项目布局。我已经开始有一段时间的性能问题了。每次我运行应用程序时,我都会在为 recyclerView 中的 View 扩
有没有办法在它的 onmeasure 中更改我的自定义 View 的 subview (或 onlayout,ondraw,我实际上不知道把它放在哪里:) 我有这段代码,它应该缩小我从 LinearL
我正在尝试根据可用空间的高度和宽度来缩放自定义 View 。所以它应该填充绿色箭头高度和宽度的空间。但是我从 heightMeasureSpec = View.MeasureSpec.getSize(
希望得到帮助。我的函数调用 onMeasure 的问题。在我的 xml 文件中(RelativeLayput)- 3 个元素:TextView、RelativeLayout 并将它们包含在我的类中。
我有一个非常简单的自定义 View ,覆盖了 onMeasure()。我的onMeasure() 根据背景Drawable 决定大小。首次从 xml 创建 View 时,一切正常。但是后来我以编程方式
我制作了一个覆盖了 onDraw() 的自定义 View ,它在 Canvas 上绘制位图。当我在布局文件中指定我想要 wrap_content 时,它仍然会填满整个屏幕。 onMeasure() 是
所以我尝试将我的一些回收器 ViewHolders 重构为 ConstraintLayouts。在我这样做之后,我看到的东西让我感到震惊。展开单个 View 比通常的 LinearLayout 多花费
我需要覆盖 onMeasure 方法,但在尝试覆盖时出现错误:DrawLnClass.DrawLn 类型的方法 OnMeasure(int, int) 必须覆盖父类(super class)方法 这是
我正在尝试修复阻止我的 Android 应用程序运行的异常。我用谷歌搜索了这个异常,发现其他人也有同样的问题。有些人通过为他们的 RecycledView 包含一个 LinearLayoutManag
什么时候 View.onMeasure(int widthMeasureSpec, int heightMeasureSpec) 打电话了?我有一个 Activity需要在 onMeasure 之后执
我有一项 Activity 和一种 View 。此 View 的目的是显示方形网格。正方形边长等于屏幕宽度除以水平存在的正方形数量。 @Override protected void onCreate
我编写了自己的布局(子类化 RelativeLayout)。我从调试器中看到 onMeasure() 和 onLayout() 被连续调用。这是正常行为吗?堆栈跟踪从 dispatchMessage(
在我当前的应用程序中,我有很多小 View 相互叠加,以至于方法分析表明测量等占了设置后应用程序一般使用的大部分 cpu 时间。 鉴于这些 View 都没有改变其父 View 中的大小或位置,是否可以
我正在做一个非常简单的功能,比如在 TextView 的末尾添加省略号。 我可以在 onMeasure()、onLayout() 和 OnGlobalLayoutListener() 调用中添加该功能
我有一个 Activity ,其中有几个线性布局的 View ,一个是 TextView ,一个是我的游戏的主视图,这是我在代码中使用的自定义 View 。 当我将以下代码添加到我的 Activity
我有一个 SurfaceView。我只是从其他线程手动绘制它。 运行良好。但是平均运行 10-20 分钟后,我得到了这个异常: 01-14 08:51:25.000 3740-3740/com.
有一个特定大小的 TextView,每当设置给它的文本太长时,我想将它重新设置为某个预设值。 为了实现这一点,我重写了 onMeasure 并调用了 setText。但是,这不会影响 TextView
编辑:当我对此进行调试并尝试了各种更改时,我意识到我发布的原始代码并不真正相关,所以我删除了它: 我有一个 Activity可以在 3 个不同的 Fragment 之间切换s 当用户按下 3 个 Ic
我是一名优秀的程序员,十分优秀!