2015-05-07 11 views
11

Mi piacerebbe ottenere il disegno di un diagramma proprio come l'immagine allegata, ma ho difficoltà a disegnare il rettangolo verticale rosso sulla destra insieme a mettere altri oggetti in cima. La più grande preoccupazione è il fare con numerose dimensioni dello schermo differenti dei dispositivi Android. Comprendo appieno ciò che sto cercando di ottenere durante il processo, che include gli obiettivi di seguito. Tutto l'aiuto sarebbe molto apprezzato.Disegna diagramma a oggetti multipli in frammento

  • 1 rettangolo rosso su entrambi i lati dello schermo (a destra non so come disegnare lì)
  • 7 caselle grigie tra i rettangoli verticali rosse devono essere uguali in larghezza
  • una linea verticale nera deve essere tra i rettangoli proprio come nell'immagine sopra
  • una casella di testo che mostra un numero deve essere al centro di ciascun rettangolo grigio insieme a quelli rossi
  • Voglio anche essere in grado per riutilizzare lo schema in futuro in modo da poter riempire le piccole scatole con rosso o nero ogni volta che vogliono

enter image description here

layout

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <my.package.name.ComplexDiagram 
      android:layout_width="match_parent" 
      android:layout_height="65dp" 
      android:layout_centerVertical="true" 
      /> 
    </RelativeLayout> 

Java

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.RectF; 
import android.util.AttributeSet; 
import android.view.View; 

public class ComplexDiagram extends View { 


    private int measuredWidth, measuredHeight; 
    private Paint mGreyRectPaint, mBlackLinePaint, mRedRectPaint; 
    private RectF mGreyRect, mBlackLineF, mRedRectF; 


    public ComplexDiagram(Context context) { 
     super(context); 
     init(context, null, 0); 
    } 

    public ComplexDiagram(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context, attrs, 0); 
    } 

    public ComplexDiagram(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(context, attrs, defStyleAttr); 
    } 

    private void init(Context context, AttributeSet attributeSet, int defStyle) { 

     mGreyRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     mGreyRectPaint.setColor(0xFF3C3C3C); 
     mGreyRectPaint.setStyle(Paint.Style.FILL); 

     mBlackLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     mBlackLinePaint.setColor(0xFF000000); 
     mBlackLinePaint.setStyle(Paint.Style.FILL); 

     mRedRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     mRedRectPaint.setColor(0xFFCC3333); 
     mRedRectPaint.setStyle(Paint.Style.FILL); 
    } 


    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     measuredHeight = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec); 
     measuredWidth = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); 

     setMeasuredDimension(measuredWidth, measuredHeight); 
    } 

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

     if (measuredHeight == 0 || measuredWidth == 0) 
      return; 

     canvas.drawRect(mGreyRect, mGreyRectPaint); 
     canvas.drawRect(mBlackLineF, mBlackLinePaint); 
     canvas.drawRect(mRedRectF, mRedRectPaint); 
    } 
} 
+0

Do scatola 1 e 7 necessità di avere 6 rettangoli, o possono utilizzare 8 come il resto di loro? –

+0

Devono avere 6 esattamente come nell'immagine. Ci sono altri disegni che mi piacerebbe realizzare quindi penso che dovremmo discuterne in chat, così posso mostrarvelo. :-) – MacaronLover

+0

@BojanKseneman Mi è stato detto prima di non sovraccaricare l'attività con le visualizzazioni e che è meglio farlo in 1 classe. Sto pensando più al metodo '1 classe', tuttavia vedrei entrambi i metodi disegnati e sceglierò tra le 2 opzioni. – MacaronLover

risposta

5

farlo in questo modo

mettere questo nel vostro XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:orientation="horizontal" > 

    <View 
     android:layout_width="5dp" 
     android:layout_height="wrap_content" 
     android:background="#CC3333" /> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#808080" > 

     <View 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="1dp" 
      android:background="@drawable/red_background" /> 

     <View 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_centerHorizontal="true" 
      android:background="@drawable/red_background" /> 

     <View 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentRight="true" 
      android:background="@drawable/red_background" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:gravity="center" 
      android:text="1" 
      android:textColor="@android:color/black" /> 

     <View 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="1dp" 
      android:background="@drawable/red_background" /> 

     <View 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:background="@drawable/red_background" /> 

     <View 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:background="@drawable/red_background" /> 
    </RelativeLayout> 

    <View 
     android:layout_width="1dp" 
     android:layout_height="wrap_content" 
     android:background="#1D1D1D" /> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#808080" > 

     <View 
      android:id="@+id/box1" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="1dp" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box2" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_marginLeft="2dp" 
      android:layout_toRightOf="@+id/box1" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box3" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_marginRight="2dp" 
      android:layout_toLeftOf="@+id/box4" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box4" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentRight="true" 
      android:background="#CC3333" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:gravity="center" 
      android:text="2" 
      android:textColor="@android:color/black" /> 

     <View 
      android:id="@+id/box5" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box6" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_marginLeft="2dp" 
      android:layout_toRightOf="@+id/box5" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box7" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_marginRight="2dp" 
      android:layout_toLeftOf="@+id/box8" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box8" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:background="@drawable/red_background" /> 
    </RelativeLayout> 

    <View 
     android:layout_width="1dp" 
     android:layout_height="wrap_content" 
     android:background="#1D1D1D" /> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#808080" > 

     <View 
      android:id="@+id/box1" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="1dp" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box2" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_marginLeft="2dp" 
      android:layout_toRightOf="@+id/box1" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box3" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_marginRight="2dp" 
      android:layout_toLeftOf="@+id/box4" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box4" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentRight="true" 
      android:background="@drawable/red_background" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:gravity="center" 
      android:text="3" 
      android:textColor="@android:color/black" /> 

     <View 
      android:id="@+id/box5" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box6" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_marginLeft="2dp" 
      android:layout_toRightOf="@+id/box5" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box7" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_marginRight="2dp" 
      android:layout_toLeftOf="@+id/box8" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box8" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:background="@drawable/red_background" /> 
    </RelativeLayout> 

    <View 
     android:layout_width="1dp" 
     android:layout_height="wrap_content" 
     android:background="#1D1D1D" /> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#808080" > 

     <View 
      android:id="@+id/box1" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="1dp" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box2" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_marginLeft="2dp" 
      android:layout_toRightOf="@+id/box1" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box3" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_marginRight="2dp" 
      android:layout_toLeftOf="@+id/box4" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box4" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentRight="true" 
      android:background="@drawable/red_background" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:gravity="center" 
      android:text="4" 
      android:textColor="@android:color/black" /> 

     <View 
      android:id="@+id/box5" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box6" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_marginLeft="2dp" 
      android:layout_toRightOf="@+id/box5" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box7" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_marginRight="2dp" 
      android:layout_toLeftOf="@+id/box8" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box8" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:background="@drawable/red_background" /> 
    </RelativeLayout> 

    <View 
     android:layout_width="1dp" 
     android:layout_height="wrap_content" 
     android:background="#1D1D1D" /> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#808080" > 

     <View 
      android:id="@+id/box1" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="1dp" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box2" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_marginLeft="2dp" 
      android:layout_toRightOf="@+id/box1" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box3" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_marginRight="2dp" 
      android:layout_toLeftOf="@+id/box4" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box4" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentRight="true" 
      android:background="@drawable/red_background" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:gravity="center" 
      android:text="5" 
      android:textColor="@android:color/black" /> 

     <View 
      android:id="@+id/box5" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box6" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_marginLeft="2dp" 
      android:layout_toRightOf="@+id/box5" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box7" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_marginRight="2dp" 
      android:layout_toLeftOf="@+id/box8" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box8" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:background="@drawable/red_background" /> 
    </RelativeLayout> 

    <View 
     android:layout_width="1dp" 
     android:layout_height="wrap_content" 
     android:background="#1D1D1D" /> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#808080" > 

     <View 
      android:id="@+id/box1" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="1dp" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box2" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_marginLeft="2dp" 
      android:layout_toRightOf="@+id/box1" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box3" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_marginRight="2dp" 
      android:layout_toLeftOf="@+id/box4" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box4" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentRight="true" 
      android:background="@drawable/red_background" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:gravity="center" 
      android:text="6" 
      android:textColor="@android:color/black" /> 

     <View 
      android:id="@+id/box5" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box6" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_marginLeft="2dp" 
      android:layout_toRightOf="@+id/box5" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box7" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_marginRight="2dp" 
      android:layout_toLeftOf="@+id/box8" 
      android:background="@drawable/red_background" /> 

     <View 
      android:id="@+id/box8" 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:background="@drawable/red_background" /> 
    </RelativeLayout> 

    <View 
     android:layout_width="1dp" 
     android:layout_height="wrap_content" 
     android:background="#1D1D1D" /> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#808080" > 

     <View 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentLeft="true" 
      android:background="@drawable/red_background" /> 

     <View 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_centerHorizontal="true" 
      android:background="@drawable/red_background" /> 

     <View 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="1dp" 
      android:background="@drawable/red_background" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:gravity="center" 
      android:text="7" 
      android:textColor="@android:color/black" /> 

     <View 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:background="@drawable/red_background" /> 

     <View 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:background="@drawable/red_background" /> 

     <View 
      android:layout_width="10dp" 
      android:layout_height="10dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="1dp" 
      android:background="@drawable/red_background" /> 
    </RelativeLayout> 

    <View 
     android:layout_width="5dp" 
     android:layout_height="wrap_content" 
     android:background="#CC3333" /> 

</LinearLayout> 

per rectangle.xml rosso

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

    <solid android:color="@android:color/transparent" /> 

    <stroke 
     android:width="1dip" 
     android:color="#CC3333" /> 

</shape> 
+0

Conosci una soluzione adeguata per risolvere [questo altro problema a livello di codice (in Java utilizzando canvas non XML)] (http://stackoverflow.com/questions/32037260/how-to-add-rectangles-on-top- di-esistente rettangolo-in-tela)? Ho passato mesi a cercare di risolvere questo problema, ma non ho avuto fortuna e le risposte fornite non risolvono il problema. – MacaronLover

Problemi correlati