没有在类中通过Canvas编辑ImageView的onTouchEvent

时间:2016-08-18 12:29:01

标签: java android xml imageview ontouchevent

我在类中使用onTouchEvent时遇到问题,它在mainActivity中编辑ImageView

.XML文件我使用这个ImageView                                               

这是在imageview上写/重写的类

public class ItemsInPacagesImageView  {

int vsk,vsk2, srk,vskconst,sizetext;
Canvas c;
Paint paint;
ImageView imageview;
Typeface font;
myTrip prvy;
String nazovtripu;
Context context;




public ItemsInPacagesImageView(ImageView img,Context context,String nazovtripu,int pacage) {
    //super(context);

    this.imageview=img;
    this.nazovtripu=nazovtripu;
    this.context=context;

    prvy= DeSerializuj(nazovtripu);
  //  Serializuj(prvy,nazovtripu);



    vsk = context.getResources().getDimensionPixelSize(R.dimen.vyskalist);
    if (prvy.getPocItem()!=pacage){
    vsk= vsk*prvy.getPocItem();}
    vsk2 = context.getResources().getDimensionPixelSize(R.dimen.vyskatext);
    srk =  context.getResources().getDimensionPixelSize(R.dimen.sirkafulllist);
    vskconst =  context.getResources().getDimensionPixelSize(R.dimen.vyskalist2);
    sizetext = context.getResources().getDimensionPixelSize(R.dimen.sizetext);
    font = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf");
    img.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, vsk));


    Bitmap b = Bitmap.createBitmap(srk,
            vsk, Bitmap.Config.ARGB_8888);
    c = new Canvas(b);
    c.drawColor(Color.WHITE);
    imageview.setImageBitmap(b);


    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);



    for (int i=1;i<50;i++) {
        if (prvy.citajItem(0,i)!=null) {
            int pom=vsk2 + (i-1)*vskconst;
            DalsiPrvok(paint, c, prvy.citajItem(0,i), 450, pom, sizetext,font);
           // prvy.setPocItem(prvy.getPocItem()+1);
        } else {break;}

    }
    imageview.invalidate();

}

protected void DalsiPrvok(Paint paint, Canvas canvas, String text, float xx, float yy, double aa, Typeface font) {

     paint.setColor(Color.BLACK);
    paint.setTypeface(font);
    float textSize = paint.getTextSize();
    paint.setTextSize((float) aa);
    canvas.drawText(text, xx, yy, paint);
    paint.setTextSize(textSize);

}


public boolean onTouchEvent(MotionEvent event) {
    int e = event.getAction(); // vrati parametre udalosti
    if (e == MotionEvent.ACTION_DOWN) {

        paint.setColor(Color.YELLOW);
        c.drawRect(0,0,srk,vsk,paint);
        imageview.invalidate();


    }
    //  HighScoreZ();


    return false;

}


public myTrip DeSerializuj( String odkial) {
    odkial=odkial+".dat";
    myTrip   prvy = null;
    try {
        File file = new File(context.getFilesDir(), odkial);
        FileInputStream fi = new FileInputStream(file);
        ObjectInputStream oi = new ObjectInputStream(fi);
        prvy = (myTrip) oi.readObject();
        oi.close();
        fi.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return prvy;
}

public void Serializuj( myTrip serTrip,String kam){
    kam=kam + ".dat";
    try {
        File file = new File(context.getFilesDir(), kam);
        FileOutputStream fo = new FileOutputStream(file);
        ObjectOutputStream ou = new ObjectOutputStream(fo);
        ou.writeObject(serTrip);
        ou.close();
        fo.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我的onTouchEvent有一个薄信,因为如果程序运行我就不能使用onTouchEvent。我尝试扩展ImageView或View或我的MainActivity。 如果我延伸↑onTouchEvent是好信,但我没有在onTouchEvent上获得代码。

所有代码运行良好,此类在Mainactivity上的Imageview上写入。但我需要实现onTouchEvent ......

我在MainActivity上调用了这个类..

ItemsInPacagesImageView itemimg= new ItemsInPacagesImageView(imglist1,tentocontext,druhy.getNazovTripu(),0);

请问如果有一些想法ako解决我的问题帮助我。

由于

1 个答案:

答案 0 :(得分:0)

我解决了这段代码的问题。如果我单击Imageview,此方法会在ItemsInPacagesImageView.class上发送sourondings x和y并重写imageview。

 imglist1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
            prvy=DeSerializuj2(nazovtripu);

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    itemimg.PociatocneBody(event.getX(),event.getY());

                    break;
                case MotionEvent.ACTION_MOVE:

                    break;
                case MotionEvent.ACTION_UP:
                    itemimg.KonecneBody(event.getX(),event.getY());
                    try {
                        prvy.Zbalene(ktore, itemimg.Spracuj());
                    } catch(IndexOutOfBoundsException e) {}
                    prvy.setHotovo(0);
                    for (int i=1;i<100;i++) {
                        prvy.Kolkomam(0,i);
                    }

                     hotovo1.setText(prvy.getHotovo()+"/"+prvy.getPocItem());



                    break;
            }

            return true;
        }
    });