2016-06-30 1 views
1

J'utilise vue loupe pour mon application qui est une bibliothèque que je suis d'ici:Comment apporter la toile au-dessus d'autres vues

https://github.com/nomanr/android-image-magnifier

J'ai modifié cette classe pour étendre FrameLayout (Il était ImageView avant) pour travailler sur mon FrameLayout.

Cela fonctionne bien, sauf que la vue de toile peinte reste en arrière de toutes les vues qui sont ajoutées dans cette vue personnalisée.

Comment amener cette toile devant ces vues ajoutées?

classe de vue personnalisée que j'utilise:

public class ImageMagnifier extends FrameLayout { 

private PointF zoomPos; 
private boolean zooming = false; 
private Matrix matrix; 
private Paint paint; 
private Bitmap bitmap; 
private BitmapShader shader; 
private int sizeOfMagnifier = 300; 
int cheight, cwidth; 

Context con; 
C c = C.getInstance(); 
public ImageMagnifier(Context context) { 
    super(context); 
    init(); 
    con=context; 
} 

public ImageMagnifier(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    init(); 
    con=context; 
} 

public ImageMagnifier(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(); 
    con=context; 
} 

private void init() { 
    zoomPos = new PointF(0, 0); 
    matrix = new Matrix(); 
    paint = new Paint(); 

    cwidth = c.Width * 109/1280; 
    cheight = cwidth * 134/109; 
} 

public void otherTouch(int x, int y) { 
    if (x > c.getwidth1(28) && x < c.getwidth1(921) && y > c.getheight1(135) && y < c.getheight1(560)) { 
     zoomPos.x = x - 10; 
     zoomPos.y = y - 75; 
     zooming = true; 
     this.invalidate(); 

    } else { 
     RemoveMagnifire(); 
    } 
} 

public void RemoveMagnifire() { 
    zooming = false; 
    this.invalidate(); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    if (!zooming) { 
     buildDrawingCache(); 
    } else { 

     bitmap = getDrawingCache(); 
     shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);   
     paint.setShader(shader); 
     matrix.reset(); 
     matrix.postScale(2f, 2f, zoomPos.x-10, zoomPos.y+60); 

     paint.getShader().setLocalMatrix(matrix); 

     int width = c.Width; 
     int height = c.Height; 

     float leftX = zoomPos.x - ((width * 100)/1280); 
     float topY = zoomPos.y - ((height * 250)/720); 
     float rightX = zoomPos.x + ((width * 100)/1280); 
     float bottomY = zoomPos.y - ((height * 100)/720); 

     canvas.drawRect(leftX , topY, rightX, bottomY, paint); 


    } 
} 

} 
+0

Veuillez poster votre classe modifiée, afin que nous puissions être sûrs de ce que vous voulez dire. –

+0

ici c'est :: http://pastebin.com/AzcVEWG7 – Prashant

+1

OK, le correctif est assez facile, mais s'il vous plaît poster la classe dans la question elle-même. S'il vous plaît ne pas lier au code hors site. Les futurs utilisateurs devront être en mesure de le voir, et ne pourront pas le faire après que ce lien soit mort. Merci. –

Répondre

1

Un ViewGroup tire ses enfants View s dans la méthode dispatchDraw(). Nous avons juste besoin de déplacer le dessin de la loupe pour que cela arrive.

La correction est simple. Déplacer tout après l'appel super dans onDraw() pour après l'appel super dans dispatchDraw().

... 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    // Removed 
} 

@Override 
protected void dispatchDraw(Canvas canvas) { 
    super.dispatchDraw(canvas); 

    if (!zooming) { 
     buildDrawingCache(); 
    } 
    else { 
     bitmap = getDrawingCache(); 
     shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 
     paint.setShader(shader); 
     matrix.reset(); 
     matrix.postScale(2f, 2f, zoomPos.x - 10, zoomPos.y + 60); 

     paint.getShader().setLocalMatrix(matrix); 

     int width = c.Width; 
     int height = c.Height; 

     float leftX = zoomPos.x - ((width * 100)/1280); 
     float topY = zoomPos.y - ((height * 250)/720); 
     float rightX = zoomPos.x + ((width * 100)/1280); 
     float bottomY = zoomPos.y - ((height * 100)/720); 

     canvas.drawRect(leftX , topY, rightX, bottomY, paint); 
    } 
} 

... 

Vous pouvez simplement supprimer la onDraw() override, si vous ne avez plus besoin pour rien d'autre.

+0

ok, son travail merci pour la solution. comment pouvons-nous supprimer la transparence de la toile? drawRect() dessine un rectangle parfaitement mais avec de la transparence. – Prashant

+1

Ouais, ça va le faire si vous n'avez pas défini un arrière-plan pour votre 'View' personnalisé, car il aura un fond transparent par défaut. Vous pouvez dessiner un autre 'Rect' juste avant l'autre, avec les mêmes dimensions et l'emplacement, mais avec un objet' Paint' différent, avec sa couleur définie comme vous voulez, avec la méthode 'Paint # setColor()'. –

+0

ok, je l'ai eu. J'utilisais une image transparente comme arrière-plan. – Prashant