【发布时间】:2016-08-18 12:29:00
【问题描述】:
我在在 mainActivity 中编辑 ImageView 的类中使用 onTouchEvent 时遇到问题
.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);
如果有一些想法可以解决我的问题,请帮助我。
谢谢
【问题讨论】:
-
我尝试实现“实现 GestureDetector.OnGestureListener、GestureDetector.OnDoubleTapListener”,但如果我尝试调试程序无法跳转到检测器代码。
标签: java android xml imageview touch-event