无法正常通过TextView的细胞在Android中画出了gridview的元素线 [英] Unable to draw line over gridview elements properly through textview cells in Android

查看:250
本文介绍了无法正常通过TextView的细胞在Android中画出了gridview的元素线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现词搜索应用程序。作为实施的一部分,我所遇到的画布和绘图线在网格视图细胞,表明用户正在触摸的手指字母组成的单词(即形成字字母)。

我已经成功了一部分,作为现在我可以画一条线以上的网格视图字母开头,但该行不通过意见网格视图中心。

请任何人能帮助我与您的宝贵意见。

有一眼下面的屏幕截图得到一个清晰的概念。


编辑:我张贴code让我如何实现它的想法

XML:

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID /根
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:后台=#FFF>

< RelativeLayout的
    机器人:ID =@ + ID /顶栏
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentTop =真
    机器人:后台=#A9E2F3>

    <的LinearLayout
        机器人:ID =@ + ID /顶栏
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:后台=#336699
        机器人:方向=横向>

        <按钮
            机器人:ID =@ + ID / btn_pause
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=暂停/>

        <天文台表
            机器人:ID =@ + ID / chronometer1
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=天文台/>

        <的TextView
            机器人:ID =@ + ID /计数器
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=center_horizo​​ntal
            机器人:TEXTSIZE =20SP
            机器人:字体=衬/>
    < / LinearLayout中>
< / RelativeLayout的>



<的FrameLayout
    机器人:ID =@ + ID / gridFrame
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_above =@ + ID /的WTable
    机器人:layout_below =@ + ID / textDisplay形式>

    < GridView控件
        机器人:ID =@ + ID /格
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:后台=#E7E8E9
        机器人:fitsSystemWindows =真
        机器人:重力=中心
        机器人:horizo​​ntalSpacing =10SP
        机器人:为numColumns =10
        机器人:填充=1DP
        机器人:stretchMode =columnWidth中
        机器人:verticalSpacing =10SP>
    < / GridView的>
< /的FrameLayout>

< GridView控件
    机器人:ID =@ + ID /的WTable
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:后台=#FFF
    机器人:为numColumns =3
    机器人:方向=垂直/>


  < / RelativeLayout的>
 

该漆绘制了包含网格视图框架布局。网格视图元素,通过自定义文本视图文件打印。

要画一条线我已经使用LineView.java

 进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.util.Log;
进口android.view.View;

公共类LineView扩展视图{

公共静态最终浮动LINE_WIDTH = 30.0f;
公共涂料粉刷=新的油漆();
保护上下文的背景下;

公众持股量startingX,startingY,endingX,endingY;

公共LineView(上下文的背景下){
    超(上下文);
    this.context =背景;
    paint.setColor(Color.parseColor(#2E9AFE));
    paint.setStrokeWidth(LINE_WIDTH);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeCap​​(Paint.Cap.ROUND);
    paint.setDither(真正的);
    paint.setAntiAlias​​(真正的);
    paint.setStyle(Paint.Style.FILL);
    paint.setAlpha(90);

}

公共无效设定值(STARTX浮动,浮动startY,浮endX,浮恩迪){

    startingX = STARTX;
    startingY = startY;
    endingX = endX;
    endingY =恩迪;
    无效();
}

@覆盖
公共无效的OnDraw(帆布油画){
    Log.e(LINEVIEW,startingX+ startingX +startingY:+ startingY);
    Log.e(LINEVIEW,endingX+ endingX +endingY:+ endingY);
    // canvas.drawLine(startingX,startingY,endingX,endingY,油漆);
    canvas.drawLine(startingX,startingY,endingX,endingY,油漆);

}
}


主要活动,其中的逻辑是执行:
这里只写了所需的逻辑。

 newGrid =(GridView控件)findViewById(R.id.grid);

    newGrid.setAdapter(新FormTheGridLetters());

    newGrid.setOnTouchListener(新OnTouchListener(){

        @覆盖
        公共布尔onTouch(视图V,MotionEvent事件){
            //如果(mp.isPlaying()){
            // mp.stop();
            //}

            INT行动= event.getActionMasked();
            开关(动作){
                案例MotionEvent.ACTION_DOWN:
                案例MotionEvent.ACTION_MOVE:
                案例MotionEvent.ACTION_UP:
                    // 数据
                    PaintViewHolder newPaint =新PaintViewHolder();
                    newPaint.DrawLine =新LineView(WordSearchActivity.this);
                    gridFrame.addView(newPaint.DrawLine);
                    buildWord =新的StringBuilder();
                    INT X =(int)的event.getX();
                    INT Y =(INT)event.getY();
                    //测试=新LineView(WordSearchActivity.this);
                    INT位置= newGrid.pointToPosition(X,Y);
                    点之一,
                    二;

                    如果(位置!= GridView.INVALID_POSITION){

                        。v.getParent()requestDisallowInterceptTouchEvent(真正的);
                        cellView =(TextView中)newGrid.getChildAt(位置);

                        。字符串= cellView.getText()的toString();
                        // Log.v(>>>>>&其中;&其中;&其中;&其中;&其中;&其中;&所述; ????????,a.toString());
                        开关(动作){
                            案例MotionEvent.ACTION_DOWN:
                                STARTX = event.getX();
                                startY = event.getY();


                                打破;
                            案例MotionEvent.ACTION_MOVE:


                                打破;
                            案例MotionEvent.ACTION_UP:
                              //检查列表形成字;
                                                       //如果发现是画
                                为(中间体的i1 = 0; i1的&其中; Ans.size(); I1 +)
                                {
                                    如果(formedWord.equals(Ans.get(I1)))
                                    {


                     answerAdapter.notifyDataSetChanged();
                     newPaint.DrawLine.setPoints(STARTX,startY,X,Y);
               //通过传递起点和终点画的字母


                                    }

                                }


                                打破;
                        }
                    } 其他 {
                        如果(mSelecting){
                            mSelecting = FALSE;
                        }
                    }
                    打破;
                案例MotionEvent.ACTION_CANCEL:
                    mSelecting = FALSE;
                    打破;
            }
            返回true;
        }
    });
 

解决方案

我不知道,如果你解决这个问题,但我会反正答案可能有这类问题的人。后您收到有效的位置,就可以得到该视图的中心,你可以设置这些值作为抽签开始。

这样的:

 如果(位置!= GridView.INVALID_POSITION){

        MyList.add(位置);
        。v.getParent()requestDisallowInterceptTouchEvent(真正的);
        TextView的cellView =(TextView中)gridView.getChildAt(位置);
         CENTREX = cellView.getX()+ cellView.getWidth()/ 2;
         centreY = cellView.getY()+ cellView.getHeight()/ 2;


    开关(event.getAction()){
        案例MotionEvent.ACTION_DOWN:

            newPaint.DrawLine.touch_start(X,Y​​,CENTREX,centreY);
 

我已经试过这code和其工作正常。我不认为你需要了,但我也加入了这个网站最近,所以也许这将帮助其他人。我可以发布更多的code,如果你想,但

  newPaint.DrawLine.touch_start(X,Y​​,CENTREX,centreY);
 

时的伎俩这个问题。 希望这有助于。

I would like to implement Word Search app. As part of implementation i have come across canvas and drawing line over grid view cells( letters that form the word) to indicate that user is touching finger over letters to form the word.

I have succeeded partially as of now i can draw a line over letters of grid view but the line is not through center of views of grid View.

Please can anyone assist me with your valuable suggestions .

Have a glance on below screen shot to get a clear idea.


Edited: I'm posting code to get an idea of how I'm implementing it.

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>

The paint is drawing over frame layout which contains grid view. Grid view elements are printed through custom text view file.

To draw a line i have used 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;
        }
    });

解决方案

I don't know if you fix the issue but I will answer anyway for the people that may have these kind of problems. After you recieve the valid position, you can get the center of the view and you can set these values as beginning of the draw.

Like this:

    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);

I have tried this code and it is working. I don't think that you need anymore but I have joined this site recently so maybe it will help other people. I can post more code if you want but

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

is the trick for that issue. Hope this helps.

这篇关于无法正常通过TextView的细胞在Android中画出了gridview的元素线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆