android – 在iPhone中为EditText放大玻璃:是否可以在视图之外绘制?

android – 在iPhone中为EditText放大玻璃:是否可以在视图之外绘制?,第1张

概述我正在尝试制作FastSelectEditText,以便: >可以通过长按选择文本并滑动手指. >滑动和选择时,显示放大玻璃(如iphone),以便用户可以在手指下看到文字. 不幸的是我的设计存在问题:MagGlass只显示在我的FastSelectEditText中.当用户在顶行选择文本时,她看不到mag玻璃. 所以我必须使用这个工作:当它到达FastSelectEditText的顶部时,显示低 @H_301_0@ @H_301_0@ 我正在尝试制作FastSelectEditText,以便:

>可以通过长按选择文本并滑动手指.
>滑动和选择时,显示放大玻璃(如iphone),以便用户可以在手指下看到文字.

不幸的是我的设计存在问题:MagGlass只显示在我的FastSelectEditText中.当用户在顶行选择文本时,她看不到mag玻璃.
所以我必须使用这个工作:当它到达FastSelectEditText的顶部时,显示低于手指的mag玻璃.

我明白如果我使用Mag Glass的另一个视图,这不会有问题.但是为了保持代码简单,我认为最好将Mag Glass保留在FastSelectEditText中.

有没有办法在视图范围之外绘制一些东西?
或者我应该编写另一个视图(而不是自定义EditText中的一些代码)来实现Mag Glass?(并且可能将这些视图放在框架布局中?)

  

public class FastSelectEditText extends EditText implements OnLongClickListener {/** * @param context */public FastSelectEditText(Context context) {    super(context);    init();}/** * @param context * @param attrs */public FastSelectEditText(Context context,AttributeSet attrs) {    super(context,attrs);    init();}/** * @param context * @param attrs * @param defStyle */public FastSelectEditText(Context context,AttributeSet attrs,int defStyle) {    super(context,attrs,defStyle);    init();}private MagGlass mMagGlass;private float mScale;private voID init(){    displayMetrics metrics = getResources().getdisplayMetrics();    mScale = metrics.density;    setGravity(Gravity.top);    setonLongClickListener(this);    mMagGlass = new MagGlass();}private int getoffset(int x,int y){    Layout layout = getLayout();    int row = layout.getlineForVertical(getScrollY()+y-getpaddingtop());    return layout.getoffsetForHorizontal(row,x-getpaddingleft());}/** * the position/index when touch down. */private int mDownOffset = 0;private int moldSelStart,moldSelEnd;/** * DID the user moved his finger after down event? */private boolean mMoved = false;@OverrIDepublic boolean dispatchtouchEvent(MotionEvent event) {    int x = (int) event.getX();    int y = (int) event.getY();    mMagGlass.setobjectCenter(x,y);    boolean result;    switch (event.getAction()){    case MotionEvent.ACTION_DOWN:        moldSelStart = getSelectionStart();        moldSelEnd = getSelectionEnd();        if (moldSelStart != moldSelEnd){            startSlIDeAndSelect();        }        mDownOffset = getoffset(x,y);        return super.dispatchtouchEvent(event);    case MotionEvent.ACTION_MOVE:        result = super.dispatchtouchEvent(event);        int offset = getoffset(x,y);        if (!mMoved && mDownOffset != offset){            mMoved = true;        }        if (mSlIDeAndSelect){                        if (mMoved){                setSelection(mDownOffset,offset);            }            return true;        }        return result;    case MotionEvent.ACTION_UP:        boolean moved = mMoved;        // reset mMoved        mMoved = false;        boolean longClicked = mLongClicked;        mLongClicked = false;        if (mSlIDeAndSelect && moved){            event.setAction(MotionEvent.ACTION_CANCEL);        }        result = super.dispatchtouchEvent(event);        if (mSlIDeAndSelect){            mSlIDeAndSelect = false;            int upOffset = getoffset(x,y);            if (!moved && mDownOffset == upOffset && longClicked){                setSelection(moldSelStart,moldSelEnd);                showContextMenu();            }else{                setSelection(mDownOffset,upOffset);            }            return true;        }        return result;    case MotionEvent.ACTION_CANCEL:        mSlIDeAndSelect = false;        // reset mMoved        mMoved = false;        mLongClicked = false;        return super.dispatchtouchEvent(event);    default:        return super.dispatchtouchEvent(event);    }}protected voID startSlIDeAndSelect() {    mSlIDeAndSelect = true;    VIEwParent parent = getParent();    if (parent != null){        parent.requestdisallowIntercepttouchEvent(true);    }}private boolean mSlIDeAndSelect = false;private boolean mLongClicked = false;private Vibrator mVibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);@OverrIDepublic boolean onLongClick(VIEw v) {    if (!mMoved){        startSlIDeAndSelect();        mLongClicked = true;        mVibrator.vibrate(30);    }    return true;}@OverrIDeprotected voID onDraw(Canvas canvas) {    super.onDraw(canvas);    if (mSlIDeAndSelect){        mMagGlass.draw(canvas);    }}/** * Need a drawable.mag_glass to work. *  * @author lifurong * */class MagGlass{    private int mWIDth,mHeight;    private Bitmap mMagGlassBitmap;    private int mX,mY;    private final static int INSET = 10;    public MagGlass(){        mMagGlassBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.mag_glass);        mWIDth = mMagGlassBitmap.getWIDth();        mHeight = mMagGlassBitmap.getHeight();    }    public voID setobjectCenter(int x,int y){        mX = x;        mY = y;    }    public voID draw(Canvas canvas) {        final float left = mX-mWIDth/2.0f;        final float top = mY-mHeight/2.0f;        final float right = mX+mWIDth/2.0f;        final float bottom = mY+mHeight/2.0f;        float vTrans = 80*mScale;        int vTransSign;        int[] location = new int[2];        getLocationInWindow(location);        int topEdge = location[1]-getpaddingtop()>0? 0:-location[1]+getpaddingtop();        if (top-vTrans > topEdge){            vTransSign = -1;        }else{            vTransSign = 1;        }        canvas.translate(0,vTrans*vTransSign);        canvas.clipRect(left,top,right,bottom);        canvas.drawBitmap(mMagGlassBitmap,left,null);        canvas.clipRect(left+INSET,top+INSET,right-INSET,bottom-INSET);        FastSelectEditText.super.onDraw(canvas);    }}

}

解决方法 要在视图范围之外绘制,您需要将视图的父级clipChildren设置为false.

默认情况下,VIEwGroup将clipChildrenset设置为true,这会导致子项在剪裁到其边界的画布上绘制.

@H_301_0@ 总结

以上是内存溢出为你收集整理的android – 在iPhone中为EditText放大玻璃:是否可以在视图之外绘制?全部内容,希望文章能够帮你解决android – 在iPhone中为EditText放大玻璃:是否可以在视图之外绘制?所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存