texto studio personalizar linearlayout examples elementos ejemplos ejemplo centrar caja boton amontonados android gridview canvas draw

personalizar - linearlayout android studio ejemplo



No se ha podido dibujar correctamente la línea sobre los elementos de la vista de cuadrícula a través de las celdas textview en Android (1)

Me gustaría implementar la aplicación Word Search. Como parte de la implementación, he encontrado un lienzo y una línea de dibujo sobre las celdas de la vista de cuadrícula (letras que forman la palabra) para indicar que el usuario está tocando el dedo sobre las letras para formar la palabra.

He tenido éxito parcialmente, ahora puedo dibujar una línea sobre las letras de la vista de cuadrícula, pero la línea no está en el centro de vistas de la vista de cuadrícula.

Por favor, ¿alguien puede ayudarme con sus valiosas sugerencias?

Echa un vistazo a la captura de pantalla inferior para obtener una idea clara.

Editado: estoy publicando código para tener una idea de cómo lo estoy implementando.

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fff" > <RelativeLayout android:id="@+id/topbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:background="#A9E2F3" > <LinearLayout android:id="@+id/topbar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#336699" android:orientation="horizontal" > <Button android:id="@+id/btn_pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pause" /> <Chronometer android:id="@+id/chronometer1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Chronometer" /> <TextView android:id="@+id/counter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textSize="20sp" android:typeface="serif" /> </LinearLayout> </RelativeLayout> <FrameLayout android:id="@+id/gridFrame" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/wTable" android:layout_below="@+id/textdisplay" > <GridView android:id="@+id/grid" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#E7E8E9" android:fitsSystemWindows="true" android:gravity="center" android:horizontalSpacing="10sp" android:numColumns="10" android:padding="1dp" android:stretchMode="columnWidth" android:verticalSpacing="10sp" > </GridView> </FrameLayout> <GridView android:id="@+id/wTable" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#fff" android:numColumns="3" android:orientation="vertical" /> </RelativeLayout>

La pintura dibuja sobre el diseño del marco que contiene la vista de cuadrícula. Los elementos de la vista de cuadrícula se imprimen a través del archivo de vista de texto personalizado.

Para dibujar una línea, he usado LineView.java

import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.Log; import android.view.View; public class LineView extends View { public static final float LINE_WIDTH = 30.0f; public Paint paint = new Paint(); protected Context context; public float startingX, startingY, endingX, endingY; public LineView(Context context) { super(context); this.context = context; paint.setColor(Color.parseColor("#2E9AFE")); paint.setStrokeWidth(LINE_WIDTH); paint.setStrokeJoin(Paint.Join.ROUND); paint.setStrokeCap(Paint.Cap.ROUND); paint.setDither(true); paint.setAntiAlias(true); paint.setStyle(Paint.Style.FILL); paint.setAlpha(90); } public void setPoints(float startX, float startY, float endX, float endY) { startingX = startX; startingY = startY; endingX = endX; endingY = endY; invalidate(); } @Override public void onDraw(Canvas canvas) { Log.e("LINEVIEW", "startingX" + startingX + " startingY:" + startingY); Log.e("LINEVIEW", "endingX" + endingX + " endingY:" + endingY); // canvas.drawLine(startingX, startingY, endingX, endingY, paint); canvas.drawLine(startingX, startingY, endingX, endingY, paint); } } Main Activity where logic is implemented: Written only the required logic here. newGrid = (GridView) findViewById(R.id.grid); newGrid.setAdapter(new FormTheGridLetters()); newGrid.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // if (mp.isPlaying()) { // mp.stop(); // } int action = event.getActionMasked(); switch (action) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_UP: // data PaintViewHolder newPaint = new PaintViewHolder(); newPaint.DrawLine = new LineView(WordSearchActivity.this); gridFrame.addView(newPaint.DrawLine); buildWord = new StringBuilder(); int x = (int) event.getX(); int y = (int) event.getY(); // test = new LineView(WordSearchActivity.this); int position = newGrid.pointToPosition(x, y); Point one, two; if (position != GridView.INVALID_POSITION) { v.getParent().requestDisallowInterceptTouchEvent(true); cellView = (TextView) newGrid.getChildAt(position); String a = cellView.getText().toString(); // Log.v(">>>>><<<<<<<????????", a.toString()); switch (action) { case MotionEvent.ACTION_DOWN: startX = event.getX(); startY = event.getY(); break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: // Checking the list for formed word ; //if found that is painted for (int i1 = 0; i1 < Ans.size(); i1++) { if (formedWord.equals(Ans.get(i1))) { answerAdapter.notifyDataSetChanged(); newPaint.DrawLine.setPoints(startX, startY, x, y); // Painted the letters by passing starting and ending points } } break; } } else { if (mSelecting) { mSelecting = false; } } break; case MotionEvent.ACTION_CANCEL: mSelecting = false; break; } return true; } });


No sé si solucionas el problema, pero responderé de todos modos a las personas que puedan tener este tipo de problemas. Después de recibir la posición válida, puede obtener el centro de la vista y puede establecer estos valores como el inicio del sorteo.

Me gusta esto:

if (position != GridView.INVALID_POSITION) { MyList.add(position); v.getParent().requestDisallowInterceptTouchEvent(true); TextView cellView = (TextView) gridView.getChildAt(position); centreX = cellView.getX() + cellView.getWidth() / 2; centreY = cellView.getY() + cellView.getHeight() / 2; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: newPaint.DrawLine.touch_start(x, y,centreX,centreY);

He intentado este código y está funcionando. No creo que necesites más, pero me he unido a este sitio recientemente, así que tal vez ayude a otras personas. Puedo publicar más código si quieres pero

newPaint.DrawLine.touch_start(x, y,centreX,centreY);

es el truco para ese problema. Espero que esto ayude.