- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有方法,它应该从 RelativeLayout 创建一个位图。我的 RelativeLayout 动态创建并将所有输入 View 放入圆圈中。它看起来像这样:
public class CircleView extends RelativeLayout {
static final int centerId = 111;
private final int radius;
Bitmap returnedBitmap;
private RelativeLayout.LayoutParams createNewRelativeLayoutParams() {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ABOVE, centerId);
lp.addRule(RIGHT_OF, centerId);
return lp;
}
private View prepareElementForCircle(View elem, int distX, int distY) {
RelativeLayout.LayoutParams lp = createNewRelativeLayoutParams();
elem.measure(0, 0);
int deltaX = elem.getMeasuredWidth() / 2;
int deltaY = elem.getMeasuredHeight() / 2;
lp.setMargins(distX - deltaX, 0, 0, radius - distY - deltaY);
elem.setLayoutParams(lp);
return elem;
}
public CircleView(Context context, int radius, View[] elements) {
super(context);
this.radius = radius;
RelativeLayout.LayoutParams lpView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
this.setLayoutParams(lpView);
View center = new View(context);
center.setId(centerId);
RelativeLayout.LayoutParams lpcenter = new RelativeLayout.LayoutParams(
0, 0);
lpcenter.addRule(CENTER_HORIZONTAL);
lpcenter.addRule(CENTER_VERTICAL);
center.setLayoutParams(lpcenter);
this.addView(center);
this.addView(prepareElementForCircle(elements[0], 0, 0));
if (elements.length % 2 == 0) {
this.addView(prepareElementForCircle(elements[elements.length / 2],
0, 2 * radius));
}
if (elements.length > 2) {
for (int i = 1; i <= (elements.length - 1) / 2; i++) {
int y = i * 4 * radius / elements.length;
int x = (int) Math.sqrt(Math.pow(radius, 2)
- Math.pow((radius - y), 2));
this.addView(prepareElementForCircle(elements[i], x, y));
this.addView(prepareElementForCircle(elements[elements.length
- i], -x, y));
}
}
}
当我将多个 View 传递到我的 CircleView 和它的 setContentView 时,一切正常。但我还需要实现旋转此 CircleView 的能力。所以我需要将我的 RelativeLayout 转换为位图。我这样做:
public Bitmap getBitmapFromView() {
this.measure(MeasureSpec.makeMeasureSpec(createNewRelativeLayoutParams().width, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(createNewRelativeLayoutParams().height, MeasureSpec.UNSPECIFIED));
this.layout(0, 0, this.getMeasuredWidth(), this.getMeasuredHeight());
this.setGravity(Gravity.CENTER);
Bitmap returnedBitmap = Bitmap.createBitmap(this.getMeasuredWidth(), this.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(returnedBitmap);
this.draw(c);
return returnedBitmap;
}
结果我得到了我的观点,但它看起来很难看。所以并不是每个 View 都会显示,它们也不在圆圈中。所以我的猜测是 View 的测量行为不应该如此。也许,我应该测量所有的 child ?或者在onPreDraw()
中调用Bitmap转换方法?我已经尝试了两种解决方案,但它们对我不起作用。
最佳答案
我解决了这个问题
public Bitmap getBitmapFromView() {
DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
this.measure(MeasureSpec.makeMeasureSpec(dm.widthPixels, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(dm.heightPixels, MeasureSpec.EXACTLY));
this.layout(0, 0, this.getMeasuredWidth(), this.getMeasuredHeight());
this.setGravity(Gravity.CENTER);
Bitmap returnedBitmap = Bitmap.createBitmap(this.getMeasuredWidth(),
this.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(returnedBitmap);
this.draw(c);
return returnedBitmap;
}
所以 DisplayMetrics
解决了我的问题。
关于android - 在绘制之前测量 RelativeView 的所有 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16667309/
具体来说,我试图让 ID 为 bottomStuff 的 RelativeLayout 向下 float 到底部。我尝试将重力设置设置为底部,这是其他 SO 帖子建议的,但它仍然一直停留在顶部,这是为
我有方法,它应该从 RelativeLayout 创建一个位图。我的 RelativeLayout 动态创建并将所有输入 View 放入圆圈中。它看起来像这样: public class Circle
每当我在 XML 中创建 ImageView 时,模拟器就会崩溃。 最佳答案 您在一些 id 引用中缺少 +,layout_
正如我下面的代码所示,我有两个我想重叠的 ImageView,但较大的被系统地放在较小的下方,两者都是 android:layout_alignParentTop 设置为“true”,我认为这会导致重
我有一个设置,其中我有一些围绕中心的 ImageView,当用户单击一个按钮时,我希望这些 View 以椭圆方式围绕中心 View 旋转。我目前有一个椭圆运动,但 View 在一个奇怪的轴上旋转,而不
据我所知,AQuery 几乎是用于 Android Java 编程的 jQuery。我是一个网络程序员,所以我习惯了 jQuery,它很容易使用,所以我想做同样的事情来操纵我的应用程序的布局。 我的问
我是一名优秀的程序员,十分优秀!