Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android customview中的TextView onTouchEvent_Android_Android Canvas - Fatal编程技术网

Android customview中的TextView onTouchEvent

Android customview中的TextView onTouchEvent,android,android-canvas,Android,Android Canvas,我有一个自定义视图,在自定义视图中我创建了4个文本视图。我希望这些文本视图中的每一个都能响应触控事件。但是,文本视图本身不响应触控事件。但是,我可以为整个自定义视图创建一个ontouch侦听器。但是,我不希望整个视图都有ontouch事件,因为我希望拖放我创建的TextView。我尝试注册ontouch事件的x和y坐标,并假设如果ontouch事件在textview的范围内,则更改textview的坐标,但这太复杂了,因为如果一个textview被拖到另一个textview的坐标,则ontouc

我有一个自定义视图,在自定义视图中我创建了4个文本视图。我希望这些文本视图中的每一个都能响应触控事件。但是,文本视图本身不响应触控事件。但是,我可以为整个自定义视图创建一个ontouch侦听器。但是,我不希望整个视图都有ontouch事件,因为我希望拖放我创建的TextView。我尝试注册ontouch事件的x和y坐标,并假设如果ontouch事件在textview的范围内,则更改textview的坐标,但这太复杂了,因为如果一个textview被拖到另一个textview的坐标,则ontouch事件将“拾取”另一个文本视图,那么我将移动两个文本视图,这不是我想要的。总之,我想知道是否可以在customview中为textview在ontouch listener中设置,如果可能,为什么不工作:

                mScale.mPositive.get(0).setOnTouchListener(new OnTouchListener()
                    {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) 
                        {
                                mScale.mCurrentXPos[0] = event.getX();
                                mScale.mCurrentYPos[0] = event.getY();
                                mScale.mDrag = true;

                            return true;
                        }

                    });
此代码适用于自定义视图,但不适用于该自定义视图中的特定文本视图

以下是自定义视图代码:

公共班级规模扩大了视野 {

public Scale(上下文、属性集属性)
{
超级(上下文,attrs);
mContext=this.getContext();
h=新处理程序();
mCalendarDbHelper=新日历适配器(mContext);
mCalendarDbHelper.open();
Cursor thinks=mCalendarDbHelper.fetchthinks();
//从数据库中创建负面想法的字符串数组
while(thinks.moveToNext())
{
if(thinks.getString(thinks.getColumnIndexOrThrow(calendardAdapter.COLUMN\u NAME\u think)).length()>0&&thinks.getString(thinks.getColumnIndexOrThrow(calendardAdapter.COLUMN\u NAME\u think)).charAt(0)='-')
{
添加(thinks.getString(thinks.getColumnIndexOrThrow(calendardAdapter.COLUMN\u NAME\u thinks));
}
}
思想。关闭();
数组大小=负的思想大小();
mBag=BitmapFactory.decodeResource(getResources(),R.drawable.bag);
mGreenBag=BitmapFactory.decodeResource(getResources(),R.drawable.green\u-bag);
对于(int i=0;i<72;i++)
{
尝试
{
mScale[i]=BitmapFactory.decodeStream(context.getAssets().open(“scale”+i+“.gif”);
}
捕获(IOE异常)
{
}
}
}
private Runnable r=new Runnable()
{
@凌驾
公开募捐{
使无效();
}
};
受保护的void onDraw(画布)
{
if(first==true)
{
宽度=this.getWidth();
height=this.getHeight();
mScale[i]=Bitmap.createScaledBitmap(mScale[i],(int)(宽*1.5),高,真);
mBag=Bitmap.createScaledBitmap(mBag,宽度/2,高度/2,真);
否定=新文本视图(mContext);
word=negative_thinks.get((int)(Math.random()*数组大小));
否定.setText(word);
负片。布局(0,0,宽/3,高/4);
负。设置重力(重心);
负数。setTextSize(15);
负片。setTextColor(颜色。黑色);
否定.setTypeface(Typeface.DEFAULT_BOLD);
负片。设置阴影层(5,2,2,彩色。白色);
否定。setDrawingCacheEnabled(真);
负。挫折背景资源(R.drawable.graycloud);
正片油漆.setColor(Color.parseColor(#FF4444”);
正片漆。设置阴影层(5,2,2,颜色。黄色);
正片画法。设置字体(字体。默认值为粗体);
正色漆,尺寸(25);
mCurrentXPos[0]=(宽度/2);
mCurrentYPos[0]=高度/4;
mCurrentXPos[1]=(宽度/2)+(宽度/8);
mCurrentYPos[1]=高度/6;
mCurrentXPos[2]=宽度/2;
mCurrentYPos[2]=高度/12;
mCurrentXPos[3]=(宽度/2)+(宽度/8);
mCurrentYPos[3]=高度/18;
mMoveXPos[0]=(宽度/2)-宽度/帧速率;
mMoveYPos[0]=(高度/4)-(高度+(高度/4))/帧速率;
mMoveXPos[1]=((宽度/2)+(宽度/8))-宽度/帧速率;
mMoveYPos[1]=(高度/6)-(高度+(高度/4))/帧速率;
mMoveXPos[2]=(宽度/2)-宽度/帧速率;
mMoveYPos[2]=(高度/12)-(高度+(高度/4))/帧速率;
mMoveXPos[3]=((宽度/2)+(宽度/8))-宽度/帧速率;
mMoveYPos[3]=(高度/18)-(高度+(高度/4))/帧速率;
mMoveByXPos[0]=-(宽度/2)/帧速率;
mmoveypos[0]=-(高度/4)/帧速率;
mMoveByXPos[1]=((宽度-(宽度/3))-(宽度/2+(宽度/8))/帧速率;
mmoveypos[1]=-(高度/6)/帧速率;
mMoveByXPos[2]=-(宽度/2)/帧速率;
mmoveypos[2]=(高度)-(高度/12))/帧速率;
mMoveByXPos[3]=((宽度-(宽度/3))-(宽度/2+(宽度/8))/帧速率;
mmoveypos[3]=(高度)-(高度/18))/帧速率;
电流x=宽度;
电流Y=高度+高度/4;
第一个=假;
}
如果(游戏结束==错误)
{
drawBitmap(mScale[i],0-(宽度/4),0,空);
drawBitmap(负.getDrawingCache(),(int)(宽度/12),(int)(高度-(高度)/2.5)-(j),null);
}
其他的
{
画布.绘图位图(mBag,宽度/4,高度/4,空);
}
如果(mMoveScale==true)
{
i++;
j+=3;
ScaleIt(canvas,i);
如果(i==21 | | i==37 | | i==53 | | i==71)
{
mMoveScale=false;
}
}
如果(跟踪器>0)
{
如果(跟踪器==1)
{
如果(currentX>width/2&¤tY>height/4&&sStop==false)
{
currentX+=mMoveXPos[0];
电流y+=mMoveYPos[0];
drawBitmap(mPositive.get(tracker-1).getDrawingCache(),currentX,currentY,null);
}
其他的
{
public Scale(Context context, AttributeSet attrs) 
{
    super(context, attrs);
    mContext = this.getContext();
            h = new Handler();
            mCalendarDbHelper=new CalendarDbAdapter(mContext);
            mCalendarDbHelper.open();
    Cursor thoughts = mCalendarDbHelper.fetchThoughts();

    //create a string array of negative thoughts from the db
        while (thoughts.moveToNext())
        {
            if (thoughts.getString(thoughts.getColumnIndexOrThrow(CalendarDbAdapter.COLUMN_NAME_THOUGHT)).length() > 0 && thoughts.getString(thoughts.getColumnIndexOrThrow(CalendarDbAdapter.COLUMN_NAME_THOUGHT)).charAt(0) == '-')
            {
                negative_thoughts.add(thoughts.getString(thoughts.getColumnIndexOrThrow(CalendarDbAdapter.COLUMN_NAME_THOUGHT)));
            }

        }
     thoughts.close();
     array_size = negative_thoughts.size();
     mBag =BitmapFactory.decodeResource(getResources(), R.drawable.bag);
     mGreenBag = BitmapFactory.decodeResource(getResources(), R.drawable.green_bag);

    for (int i = 0; i < 72; i ++)
    {
        try
        {
        mScale[i] = BitmapFactory.decodeStream(context.getAssets().open("scale_"+i+".gif"));
        }
        catch (IOException e) 
        {

        }
    }
}


private Runnable r= new Runnable() 
{

    @Override
    public void run() {
        invalidate();

    }

};


protected void onDraw (Canvas canvas)
{
    if (first == true)
    {
        width = this.getWidth();
        height = this.getHeight();
        mScale[i] = Bitmap.createScaledBitmap(mScale[i], (int) (width * 1.5), height, true);
        mBag = Bitmap.createScaledBitmap(mBag, width/2, height/2, true);
        negative = new TextView(mContext);
        word = negative_thoughts.get((int) (Math.random() * array_size));
        negative.setText(word);
        negative.layout(0, 0, width/3, height/4);
        negative.setGravity(Gravity.CENTER);
        negative.setTextSize(15);
        negative.setTextColor(Color.BLACK);
        negative.setTypeface(Typeface.DEFAULT_BOLD);
        negative.setShadowLayer(5, 2, 2, Color.WHITE);
        negative.setDrawingCacheEnabled(true);
        negative.setBackgroundResource(R.drawable.graycloud);
        positive_paint.setColor(Color.parseColor("#FF4444"));
        positive_paint.setShadowLayer(5, 2, 2, Color.YELLOW);
        positive_paint.setTypeface(Typeface.DEFAULT_BOLD);
        positive_paint.setTextSize(25);
        mCurrentXPos[0] = (width/2);
        mCurrentYPos[0] = height/4;
        mCurrentXPos[1] = (width/2) + (width/8);
        mCurrentYPos[1] = height/6;
        mCurrentXPos[2] = width/2;
        mCurrentYPos[2] = height/12;
        mCurrentXPos[3] = (width/2) + (width/8);
        mCurrentYPos[3] = height/18;
        mMoveXPos[0] = ((width/2) - width)/FRAME_RATE;
        mMoveYPos[0] = ((height/4) - (height + (height/4)))/FRAME_RATE;
        mMoveXPos[1] = (((width/2) + (width/8)) - width)/ FRAME_RATE;
        mMoveYPos[1] = ((height/6) - (height + (height/4)))/FRAME_RATE;
        mMoveXPos[2] = ((width/2) - width)/ FRAME_RATE;
        mMoveYPos[2] = ((height/12) - (height + (height/4)))/FRAME_RATE;
        mMoveXPos[3] = (((width/2) + (width/8)) - width)/ FRAME_RATE;
        mMoveYPos[3] = ((height/18) - (height + (height/4)))/FRAME_RATE;
        mMoveByXPos[0] = -(width/2)/ FRAME_RATE;
        mMoveByYPos[0] = -(height/4)/FRAME_RATE;
        mMoveByXPos[1] =  ((width - (width/3)) - (width/2 + (width/8)))/ FRAME_RATE;
        mMoveByYPos[1] = -(height/6)/FRAME_RATE;
        mMoveByXPos[2] = - (width/2)/ FRAME_RATE;
        mMoveByYPos[2] =  ((height) - (height/12))/FRAME_RATE;
        mMoveByXPos[3] =  ((width - (width/3)) - (width/2 + (width/8)))/ FRAME_RATE;
        mMoveByYPos[3] =  ((height) - (height/18))/FRAME_RATE;
        currentX = width;
        currentY = height + height/4;
        first = false;
    }


    if (game_over == false)
    {
        canvas.drawBitmap(mScale[i], 0 - (width/4), 0, null);
        canvas.drawBitmap(negative.getDrawingCache(),(int) (width/12), (int) (height - (height)/2.5) - (j), null);
    }

    else
    {
        canvas.drawBitmap(mBag, width/4, height/4, null);
    }

    if (mMoveScale == true)
    {
        i++;
        j+=3;
        ScaleIt(canvas, i);
        if (i == 21 || i == 37 || i == 53 || i == 71)
        {
            mMoveScale = false;
        }

    }

    if (tracker > 0)
    {
        if (tracker == 1)
        {
            if (currentX > width/2 && currentY > height/4 && sStop == false)
            {
                currentX += mMoveXPos[0]; 
                currentY += mMoveYPos[0];
                canvas.drawBitmap(mPositive.get(tracker -1 ).getDrawingCache(), currentX, currentY, null);

            }

            else 
            {
                if (sStop == false)
                {


                    mMoveScale = true;
                    sStop = true;
                    currentX = width;
                    currentY = height + height/4;
                    draw_em++;
                }

            }
        }


        if (tracker == 2)
        {

            if (currentX > width/2 + (width/8) && currentY > (height/6) && sStop == false)
            {
                currentX += mMoveXPos[1]; 
                currentY += mMoveYPos[1];
                canvas.drawBitmap(mPositive.get(tracker -1 ).getDrawingCache(), currentX, currentY, null);

            }

            else
            {
                if (sStop == false)
                {
                    mMoveScale = true;
                    sStop = true;
                    currentX = width;
                    currentY = height + height/4;
                    draw_em++;
                }
            }
        }


        if (tracker == 3)
        {

            if (currentX > width/2 && currentY > height/12 && sStop == false)
            {
                currentX += mMoveXPos[2]; 
                currentY += mMoveYPos[2];
                canvas.drawBitmap(mPositive.get(tracker -1 ).getDrawingCache(), currentX, currentY, null);

            }

            else 
            {
                if (sStop == false)
                {
                    mMoveScale = true;
                    sStop = true;
                    currentX = width;
                    currentY = height + height/4;
                    draw_em++;
                }
            }
        }

        if (tracker == 4)
        {

            if (currentX > width/2 + (width/8) && currentY > (height/18) && sStop == false)
            {
                currentX += mMoveXPos[3]; 
                currentY += mMoveYPos[3];
                canvas.drawBitmap(mPositive.get(tracker -1 ).getDrawingCache(), currentX, currentY, null);
            }

            else 
            {
                    if (sStop == false)
                    {
                    mMoveScale = true;
                    sStop = true;
                    game_over = true;
                    currentX = width;
                    currentY = height + height/4;
                    draw_em++;

                    }
            }
        }


        if (draw_em > 0 && game_over == false)
        {
            for (int i = 0; i < draw_em; i ++)
            {
            if (i == 0)
            {
                canvas.drawBitmap(mPositive.get(i).getDrawingCache(), width/2, height/4 + j, null);
            }
            if (i == 1)
            {
                canvas.drawBitmap(mPositive.get(i).getDrawingCache(), width/2 + (width/8), height/6 + j, null);

            }
            if (i == 2)
            {
                canvas.drawBitmap(mPositive.get(i).getDrawingCache(), width/2, height/12 + j, null);


            }
            if (i == 3)
            {
                canvas.drawBitmap(mPositive.get(i).getDrawingCache(), width/2 + (width/8), height/18 + j, null);
            }

        }

        }

        else if (game_over == true)
        {
            for (int i = 0; i < draw_em; i++)
            {
                if (i == 0 && mCurrentXPos[0] > 0 && mCurrentYPos[0] >  0 && mDrag == false)
                {

                    mCurrentXPos[0] += mMoveByXPos[0];
                    mCurrentYPos[0] += mMoveByYPos[0];
                    canvas.drawBitmap(mPositive.get(i).getDrawingCache(), mCurrentXPos[0], mCurrentYPos[0], null);
                }

                else if (i == 0 && mCurrentXPos[0] <= 0 || mCurrentYPos[0] <=  0 && mDrag == false)
                { 
                    canvas.drawBitmap(mPositive.get(0).getDrawingCache(), 0, 0, null);
                }

                else if (i == 0 && mDrag == true)
                {
                    canvas.drawBitmap(mPositive.get(0).getDrawingCache(), mCurrentXPos[0], mCurrentYPos[0], null);
                }

                if (i == 1 && mCurrentXPos[1] < (width - (mPositive.get(i).getWidth()/2)) && mCurrentYPos[1] >  mPositive.get(i).getHeight()/2)
                {
                    mCurrentXPos[1] += mMoveByXPos[1];
                    mCurrentYPos[1] += mMoveByYPos[1];
                    canvas.drawBitmap(mPositive.get(i).getDrawingCache(), mCurrentXPos[1], mCurrentYPos[1], null);

                }

                else if (i == 1 && mCurrentXPos[1] >= (width - (mPositive.get(i).getWidth()/2)) || mCurrentYPos[1] <=  mPositive.get(i).getHeight()/2)
                {
                    canvas.drawBitmap(mPositive.get(1).getDrawingCache(), width - (width/3), 0, null);
                }

                if (i == 2 && mCurrentXPos[2] > (mPositive.get(i).getWidth()/2) && mCurrentYPos[2] <  (height - mPositive.get(i).getHeight()/2))
                {
                    mCurrentXPos[2] += mMoveByXPos[2];
                    mCurrentYPos[2] += mMoveByYPos[2];
                    canvas.drawBitmap(mPositive.get(i).getDrawingCache(), mCurrentXPos[2], mCurrentYPos[2], null);
                }

                else if (i == 2 && mCurrentXPos[2] <= (mPositive.get(i).getWidth()/2) || mCurrentYPos[2] >=  (height - mPositive.get(i).getHeight()/2))
                {
                    canvas.drawBitmap(mPositive.get(2).getDrawingCache(), 0, height - (height/4), null);    
                }

                if (i == 3 && mCurrentXPos[3] < (width - (mPositive.get(i).getWidth()/2)) && mCurrentYPos[3] < (height - mPositive.get(i).getHeight()/2))
                {
                    mCurrentXPos[3] += mMoveByXPos[3];
                    mCurrentYPos[3] += mMoveByYPos[3];
                    canvas.drawBitmap(mPositive.get(i).getDrawingCache(), mCurrentXPos[3], mCurrentYPos[3], null);
                }

                else if (i == 3 && mCurrentXPos[3] >= (width - (mPositive.get(i).getWidth()/2)) || mCurrentYPos[3] >= (height - mPositive.get(i).getHeight()/2))
                {
                    canvas.drawBitmap(mPositive.get(3).getDrawingCache(), width - (width/3), height - (height/4), null);    
                }


            }
        }
    }
    h.postDelayed(r, FRAME_RATE);

}

protected void moveIt(Canvas canvas, int moveX,int moveY, int i)
{
    if (i == 0)
    {
        canvas.drawBitmap(mPositive.get(i).getDrawingCache(), moveX, moveY, null);
    }

    if (i == 1)
    {
        canvas.drawBitmap(mPositive.get(i).getDrawingCache(), moveX, moveY, null);
    }

    if (i == 2)
    {
        canvas.drawBitmap(mPositive.get(i).getDrawingCache(), moveX, moveY, null);
    }

    if (i == 3)
    {
        canvas.drawBitmap(mPositive.get(i).getDrawingCache(), moveX, moveY, null);

    }
}

protected void moveEm(Canvas canvas, int[]mMovePosX, int[] mMovePosY)
{
    for (int i = 0; i < 4; i++)
    {

    }
}

protected void ScaleIt(Canvas canvas, int i)
{
    mScale[i] = Bitmap.createScaledBitmap(mScale[i], (int) (width * 1.5), height, true);
    mScale[i-1].recycle();

}


}
 public class ScaleView extends Activity 
 {  
    Context mContext; 
Scale mScale;
EditText positive_thought;
Button fire;
TextView pos;
private static Set<String> mPositiveWords;
private static Set<String> mNegativeWords;
int count;
private Pattern four_letter_words = Pattern.compile("not|cant|cnt|can't"); 
String inputLine;
private String[] inputTokens;
Button question;
Button skip;

public static boolean populatePositiveWords(Context context)
{
    mNegativeWords = new HashSet<String>();


    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(context.getAssets().open("negative_words.txt")));          
        String line = reader.readLine();

        while (line != null)
        {
            mNegativeWords.add(line.toLowerCase(Locale.US));
            line = reader.readLine();
        }

        reader.close();
    }
    catch (IOException exception)
    {
        return false;
    }

    return true;
    //TODO list of negative words 
}



@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    this.getActionBar().hide();
    mContext = this;
    populatePositiveWords(mContext);
    setContentView(R.layout.activity_scale);
    mScale = (Scale) findViewById(R.id.anim_view);
    mScale.setClickable(true);
    positive_thought = (EditText) findViewById(R.id.thoughts);
    fire = (Button) findViewById(R.id.scale_it);
    skip = (Button) findViewById(R.id.skip);
    question = (Button) findViewById(R.id.question);
    InputFilter[] FilterArray = new InputFilter[1];
    FilterArray[0] = new InputFilter.LengthFilter(60);
    positive_thought.setFilters(FilterArray);


    fire.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View view) 
        {
            //if the button is clicked invalidate the ondraw method and pass in the text of the positive word 
            inputLine = positive_thought.getText().toString();
            inputTokens = inputLine.split(" ");

            if (inputLine.isEmpty())
            {
                Toast.makeText(mContext, "You have to write something!", Toast.LENGTH_SHORT).show();
                return;                 
            }

            if (inputTokens.length < 3)
            {
                Toast.makeText(mContext, "At least three words are required.", Toast.LENGTH_SHORT).show();
                return;
            }               

            if (four_letter_words.matcher(inputLine).find() == true)
            {
                Toast.makeText(mContext, "Make an affirmative statement!", Toast.LENGTH_SHORT).show();
                return;
            }

            boolean matchesToken = false;

            for (int i = 0; i < inputTokens.length; i++)
            {
                String token = inputTokens[i];

                if (mNegativeWords.contains(token.toLowerCase(Locale.US)))
                {
                    matchesToken = true;
                    break;
                }
            }

            if (matchesToken == true)
            {
                Toast.makeText(mContext, "Use positive words!", Toast.LENGTH_SHORT).show();
                return;
            }

            else
            {

            InputMethodManager imm = (InputMethodManager)mContext.getSystemService(
                      Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(positive_thought.getWindowToken(), 0);
            pos = new TextView (mContext);
            pos.layout(0, 0, mScale.width/3, mScale.height/4);
            pos.setGravity(Gravity.CENTER);
            pos.setTextSize(15);
            pos.setTextColor(Color.RED);
            pos.setTypeface(Typeface.DEFAULT_BOLD);
            pos.setShadowLayer(5, 2, 2, Color.YELLOW);
            pos.setText(positive_thought.getText().toString());
            pos.setDrawingCacheEnabled(true);
            pos.setBackgroundResource(R.drawable.whitecloud);
            pos.setClickable(true);
            mScale.mPositive.add(pos);
            mScale.scale_it = true;
            count++;
            mScale.sStop = false;
            mScale.tracker = count;
            if (count == 4)
            {
                ((RelativeLayout)question.getParent()).removeView(question);
                ((RelativeLayout)skip.getParent()).removeView(skip);
                mScale.mPositive.get(0).setOnTouchListener(new OnTouchListener()
                    {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) 
                        {
                                mScale.mCurrentXPos[0] = event.getX();
                                mScale.mCurrentYPos[0] = event.getY();
                                mScale.mDrag = true;
                                return true;
                        }

                    });


            }



            }
            positive_thought.setText(null);

        }
    });

}
protected void onDraw(Canvas canvs)
{
    ....
    negative = new TextView(mContext);
    ...
    canvas.drawBitmap(negative.getDrawingCache(), ...)