Search code examples
javaandroidxmlimageviewtouch-event

not working onTouchEvent in class which edit ImageView via Canvas


I have problem with use onTouchEvent in class which edit ImageView in mainActivity

.XML file I work with this ImageView

Here Are class which write/rewrite on 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();
    }
}

I have onTouchEvent with thin letter because I can't use onTouchEvent if program run. I tried extends ImageView or View or my MainActivity. If I extends ↑ onTouchEvent was good letters but I did not get code on onTouchEvent.

All code run good, thise class write on Imageview on Mainactivity all. But I need Implement into onTouchEvent...

I call thise class on MainActivity ..

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

Please If have some ideas ako resolve my problem Help me.

Thanks


Solution

  • I resolve problem with this code. If I click on Imageview this method send sourondings x and y on ItemsInPacagesImageView.class and rewrite 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;
            }
        });