Android:将任意变换应用于视图

Android:将任意变换应用于视图,第1张

概述我想采取任意矩阵并将其应用于 android.views.View. 我找到的唯一可靠的方法是这个黑客: MyAnimation animation = new MyAnimation(matrix); animation.setDuration(0); animation.setFillAfter(true); view.setAnimation(animation); 有没有更好的办法? 我想采取任意矩阵并将其应用于 android.vIEws.VIEw.

我找到的唯一可靠的方法是这个黑客:

MyAnimation animation = new MyAnimation(matrix); animation.setDuration(0); animation.setFillAfter(true); vIEw.setAnimation(animation);

有没有更好的办法?我尝试利用getChildStatictransformation并将其放在父级中,但这样做不成功(也许我做错了?)

解决方法 最后,我基于absoluteLayout创建了自己的布局,在我的LayoutParams中添加了一个Matrix,利用了getChildStatictransformation,并覆盖了dispatchtouchEvent,以便我的孩子在旋转时响应正确的边界.比我预想的要困难得多.

public class UIVIEwLayout extends VIEwGroup {@OverrIDeprotected boolean getChildStatictransformation(VIEw child,transformation t) {    if(child instanceof UIVIEwLayout) {        t.getMatrix().reset();        UIVIEwLayout.LayoutParams params = (UIVIEwLayout.LayoutParams)child.getLayoutParams();        t.settransformationType(transformation.TYPE_MATRIX);        t.getMatrix().set(params.matrix);    }    return true;}public UIVIEwLayout(androID.content.Context context) {    super(context);    this.setClipChildren(false);    this.setClipTopadding(false);    this.setChildrenDrawingOrderEnabled(true);    this.setStatictransformationsEnabled(true);}public UIVIEwLayout(Context context,AttributeSet attrs) {    super(context,attrs);}public UIVIEwLayout(Context context,AttributeSet attrs,int defStyle) {    super(context,attrs,defStyle);}@OverrIDeprotected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) {    int count = getChildCount();    int maxHeight = 0;    int maxWIDth = 0;    // Find out how big everyone wants to be    measureChildren(wIDthMeasureSpec,heightmeasureSpec);    // Find rightmost and bottom-most child    for (int i = 0; i < count; i++) {        VIEw child = getChildAt(i);        if (child.getVisibility() != GONE) {            int childRight;            int childBottom;            UIVIEwLayout.LayoutParams lp                    = (UIVIEwLayout.LayoutParams) child.getLayoutParams();            childRight = lp.x + child.getMeasureDWIDth();            childBottom = lp.y + child.getMeasuredHeight();            maxWIDth = Math.max(maxWIDth,childRight);            maxHeight = Math.max(maxHeight,childBottom);        }    }    // Account for padding too    //maxWIDth += mpaddingleft + mpaddingRight;    //maxHeight += mpaddingtop + mpaddingBottom;    // Check against minimum height and wIDth    maxHeight = Math.max(maxHeight,getSuggestedMinimumHeight());    maxWIDth = Math.max(maxWIDth,getSuggestedMinimumWIDth());    setMeasuredDimension(resolveSizeAndState(maxWIDth,wIDthMeasureSpec,0),resolveSizeAndState(maxHeight,heightmeasureSpec,0));}@OverrIDeprotected VIEwGroup.LayoutParams generateDefaultLayoutParams() {    return new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,0);}@OverrIDeprotected voID onLayout(boolean changed,int l,int t,int r,int b) {    int count = getChildCount();    for (int i = 0; i < count; i++) {        VIEw child = getChildAt(i);        if (child.getVisibility() != GONE) {            UIVIEwLayout.LayoutParams lp =                    (UIVIEwLayout.LayoutParams) child.getLayoutParams();            int childleft = lp.x;            int childtop = lp.y;            child.layout(childleft,childtop,childleft + child.getMeasureDWIDth(),childtop + child.getMeasuredHeight());        }    }}@OverrIDepublic VIEwGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {    return new UIVIEwLayout.LayoutParams(getContext(),attrs);}// OverrIDe to allow type-checking of LayoutParams.@OverrIDeprotected boolean checkLayoutParams(VIEwGroup.LayoutParams p) {    return p instanceof UIVIEwLayout.LayoutParams;}@OverrIDeprotected VIEwGroup.LayoutParams generateLayoutParams(VIEwGroup.LayoutParams p) {    return new LayoutParams(p);}@OverrIDepublic boolean shouldDelayChildpressedState() {    return false;}@OverrIDepublic boolean dispatchtouchEvent(MotionEvent ev) {    for(int i = 0; i < this.getChildCount(); i++) {        VIEw child = getChildAt(i);        if(child instanceof UIVIEwLayout) {            UIVIEwLayout.LayoutParams params = (UIVIEwLayout.LayoutParams)child.getLayoutParams();            if(!params.matrix.isIDentity()) {                MotionEvent ev2 = MotionEvent.obtain(ev);                ev2.setLocation(ev2.getX() - params.x,ev2.getY() - params.y);                Matrix m = new Matrix();                params.matrix.invert(m);                ev2.transform(m);                if(child.dispatchtouchEvent(ev2)) {                    return true;                }                ev2.recycle();            }        }    }    return super.dispatchtouchEvent(ev);}public static class LayoutParams extends VIEwGroup.LayoutParams {    public int x;    public int y;    public Matrix matrix;    public LayoutParams(int wIDth,int height,int x,int y) {        super(wIDth,height);        this.x = x;        this.y = y;        this.matrix = new Matrix();    }    public LayoutParams(Context c,AttributeSet attrs) {        super(c,attrs);    }    public LayoutParams(VIEwGroup.LayoutParams source) {        super(source);    }}}
总结

以上是内存溢出为你收集整理的Android:将任意变换应用于视图全部内容,希望文章能够帮你解决Android:将任意变换应用于视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1129393.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存