画面にドラッグされた画像を処理する子クラスがあります。各画像の名前と x、y 位置を変数に格納します。次に、それらをスケーリングし、他の操作を行うメソッドがあります。
これらのクラスを2つにしたいのですが、画像名とx、yの位置が異なります。
最初に、このクラスを子クラスとして持つクラスを作成しようとしました。この親クラスでは、子クラスで作成されたものの代わりに、これらを使用することを期待して、同じ変数を作成しようとしました。
子クラスは、親ではなく、その中で作成された変数を引き続き使用していました。
これを行う方法はありますか、それとも間違った木に登っていますか?
public class cShapeParent extends cShapes {
// would like the base class to use this data
String[] mNames=
{
"ball_green.png",
"ball_green.png",
"ball_green.png"
};
cShapeParent(Context context,int screenWidth, int screenHeight)
{
super(context, screenWidth, screenHeight);
}
}
子クラス
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
public class cShapes {
public
String[] mNames=
{
"ball_green.png",
"ball_red.png",
"ball_blue.png"
};
int[] mX={200,300,400};
int[] mY={200,200,200};
int[] mW={0,0,0};
int[] mH={0,0,0};
int Amount=3;
int MyScreenWidth=512;
int[] BitMapId={
R.drawable.ball_green, R.drawable.ball_red,
R.drawable.ball_blue, R.drawable.ball_green
};
Bitmap MyImages[];
Rect[] mBitmapSize;
cShapes(Context context,int screenWidth, int screenHeight)
{
// Load in bitmaps
MyImages=new Bitmap[4];
AssetManager assetManager= context.getAssets();
InputStream inputStream;
mBitmapSize = new Rect[Amount];
try {
// set scaling ratio
float r=(float)MyScreenWidth / (float)screenWidth;
for(int i=0;i<Amount;i++)
{
inputStream=assetManager.open( mNames[i]);
MyImages[i]=BitmapFactory.decodeStream(inputStream);
int w=(int)( (float)MyImages[i].getWidth()*r);
int h=(int)( (float)MyImages[i].getWidth()*r);
mBitmapSize[i]=new Rect(0,0,w,h);
mW[i]=w; mH[i]=h;
mX[i]=(int) ( (float)mX[i]*r);
mY[i]=(int) ( (float)mY[i]*r);
inputStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
boolean Intersect(int id, int x,int y)
{
if ( x >= mX[id] && x <= mX[id]+128 && y >= mY[id] && y <= mY[id]+128 )
return true;
return false;
}
} // end class