i have a game which have a recycle process for each level , but it will be harder by the time,
can i make a view for common feature and extend it by each level activity??
any one have a solution ? or another idea ? ( i need each level in a separated activity )
that's what i made ...
public class Prev extends Activity{
private Bitmap image1;
private Bitmap image2;
private Bitmap image3;
private Bitmap image4;
private final int IMAGE_1_X = 50;
private final int IMAGE_2_X = 450;
private final int IMAGE_3_X = 50;
private final int IMAGE_4_X = 450;
private final int IMAGE_1_Y = 50;
private final int IMAGE_2_Y = 50;
private final int IMAGE_3_Y = 300;
private final int IMAGE_4_Y = 300;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Preview p = new Preview(this);
setContentView(p);
}
public void setImages(Bitmap image1, Bitmap image2, Bitmap image3, Bitmap image4)
{
this.image1 = image1;
this.image2 = image2;
this.image3 = image3;
this.image4 = image4;
}
public void setRawsNames(String sound_1, String sound_2, String sound_3, String sound_4)
{
this.sound_1 = sound_1;
this.sound_2 = sound_2;
this.sound_3 = sound_3;
this.sound_4 = sound_4;
}
public class Preview extends SurfaceView{
public Preview(Context context)
{
super(context);
}
#Override
public void onDraw(Canvas canvas)
{
canvas.drawBitmap(image1, IMAGE_1_X, IMAGE_1_Y,null);
canvas.drawBitmap(image2, IMAGE_2_X, IMAGE_2_Y,null);
canvas.drawBitmap(image3, IMAGE_3_X, IMAGE_3_Y,null);
canvas.drawBitmap(image4, IMAGE_4_X, IMAGE_4_Y,null);
}
}
}
here i will extend the upper class
public class MainActivity extends Prev {
private Bitmap image1;
private Bitmap image2;
private Bitmap image3;
private Bitmap image4;
public MainActivity()
{
preperImages();
setImages(image1, image2, image3, image4);
}
private void preperImages()
{
image1 = BitmapFactory.decodeResource(getResources(), R.drawable.one);
image2 = BitmapFactory.decodeResource(getResources(), R.drawable.two);
image3 = BitmapFactory.decodeResource(getResources(), R.drawable.three);
image4 = BitmapFactory.decodeResource(getResources(), R.drawable.four);
}
}
Not sure what you exactly want to achieve. From what I understand, you want to reuse a view. For this, you can use include tag in your layout.
Related
I have a custom view I've created that draws a circle, places a single text character in the middle, and sets the font to a custom icon font. It looks like this
The code for it looks like this
public class IconView extends TextView
{
public static final String ICON_PROJECTOR = "A";
public static final String ICON_BLURAY = "F";
public static final String ICON_TUNER = "H";
public static final String ICON_CALL = "W";
public static final String ICON_HANG_UP = "X";
public static final String ICON_SLIDERS = "l";
public static final String ICON_KEYPAD = "t";
public static final String ICON_UP = "!";
public static final String ICON_DOWN = "#";
public static final String ICON_LEFT = "#";
public static final String ICON_RIGHT = "$";
public static final String ICON_UP_ALT = "%";
public static final String ICON_DOWN_ALT = "^";
public static final String ICON_LEFT_ALT = "&";
public static final String ICON_RIGHT_ALT = "*";
public static final String ICON_MENU_BACK = "<";
public static final String ICON_MENU_FWD = ">";
private int bgColor;
public IconView(Context context, AttributeSet attrs)
{
super(context, attrs);
Typeface iconTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/hav.ttf");
this.setTypeface(iconTypeface);
}
public void setIcon(String icon)
{
this.setText(icon);
}
public void setColor(int color)
{
bgColor=color;
}
#Override
protected void onDraw(Canvas canvas)
{
int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
this.setLayoutParams(new LinearLayout.LayoutParams(size,size));
this.setGravity(Gravity.CENTER);
//Draw Background
Rect rect = canvas.getClipBounds();
Paint paint = new Paint();
paint.setColor(bgColor);
paint.setAntiAlias(true);
canvas.drawCircle(rect.centerX(), rect.centerY(), rect.width() / 2, paint);
//Set Text Color
this.setTextColor(0xFFFFFFFF);
super.onDraw(canvas);
}
}
It runs and looks beautiful. Unfortunately, when it's running the entire application lags and my memory usage looks like this . I'm pretty sure that means something's leaking, and since I'm very new to this I don't know what in my onDraw method is causing it. If I comment out everything in onDraw but super() then I get just the icon with the font, no circle, and the memory problems go away entirely. I get a nice solid even line for memory and the app is snappy and responsive.
So what's wrong with my circle code, how can I fix it, or what should I replace it with?
Thanks for any help, I appreciate it!
I am trying to declare and initialize a variable globally as final, this variable should holde thewidth and the height of an element in the drawable, how to achieve that.
I want smothing like the followinf "ofcourse it is not working, but it is roughly what i am trying to achieve:
private final int w = R.drawable.element.getwidth
You can define a function getDrawableWidth
private int getDrawableWidth(Resources resources, int id){
Bitmap bitmap = BitmapFactory.decodeResource(resources, id);
return bitmap.getWidth();
}
Then call it as below:
private final int w = getDrawableWidth(getResources(), R.drawable.element)
===Edit===
define a class to get global context(from here):
public class App extends Application{
private static Context mContext;
#Override
public void onCreate() {
super.onCreate();
mContext = this;
}
public static Context getContext(){
return mContext;
}
}
then change getDrawableWidth to another format:
private int getDrawableWidth(int id){
Bitmap bitmap = BitmapFactory.decodeResource(App.getContext().getResources(), id);
return bitmap.getWidth();
}
use it any where:
private final int w = getDrawableWidth(R.drawable.element)
I'm current creating a game and i've made 3 main classes - GameView (which extends surfaceview), GameViewThread (which is a thread used for surfaceview) and a MainActivity.
I want to enter a background image in SurfaceView, but have not been able to figure out how to do so.
Using setBackground() in the onDraw method of GameView doesn't work as well.
I would greatly appreciate any help you guys can provide me with.
Here is my GameView class:
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private Bitmap bmp1;
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private Sprite sprite;
private float sensorX = 0;
private Platform platform[] = new Platform[7];
private Bonus GravUp[] = new Bonus[3];
private Bonus GravDown[] = new Bonus[3];
private Bitmap bmp2;
private Bitmap bmp3;
private boolean collision;
private int a;
private int b;
private int c1;
private double c2;
int agility = 50;
private Bitmap bmp4;
public double getsensorX() {
return sensorX;
}
public double setsensorX(float s) {
return sensorX = s;
}
synchronized public int getSpriteY() {
return sprite.y;
}
synchronized public int getSpriteX() {
return sprite.x;
}
synchronized public int getSpriteHeight() {
return sprite.height;
}
synchronized public int getSpriteWidth() {
return sprite.width;
}
public GameView(Context context) {
super(context);
gameLoopThread = new GameLoopThread(this);
holder = getHolder();
holder.addCallback(this);
bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.russel);
sprite = new Sprite(this);
sprite.getBMP(bmp1);
bmp2 = BitmapFactory.decodeResource(getResources(), R.drawable.brick);
bmp3 = BitmapFactory.decodeResource(getResources(), R.drawable.gravup);
bmp4 = BitmapFactory.decodeResource(getResources(), R.drawable.gravdown);
for (int i = 0; i < GravUp.length; i++) {
GravUp[i] = new Bonus(this, bmp3);
}
for (int i = 0; i < GravDown.length; i++) {
GravDown[i] = new Bonus(this, bmp4);
}
for (int i = 0; i < platform.length; i++) {
Random r = new Random();
platform[i] = new Platform(this, bmp2, 850 + 200 * i,
400 - r.nextInt(250));
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
gameLoopThread.setRunning(false);
while (retry) {
try {
gameLoopThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.YELLOW);
sprite.Draw(canvas);
for (int i = 0; i < platform.length; i++) {
platform[i].Draw(canvas);
}
for (int i = 0; i < GravUp.length; i++) {
GravUp[i].Draw(canvas);
}
for (int i = 0; i < GravDown.length; i++) {
GravDown[i].Draw(canvas);
}
}
My MainActivity Class:
public class MainActivity extends Activity {
GameView gameView;
FrameLayout game;
RelativeLayout GameButtons;
Button butOne;
Button butTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView = new GameView(this);
game = new FrameLayout(this);
GameButtons = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
GameButtons.setLayoutParams(params);
game.addView(gameView);
game.addView(GameButtons);
setContentView(game);
}
}
How can I use an image I have saved in res/drawable-hdpi folder as the background to my game?
Should I use the command in GameView or MainActivity?
Do let me know if you need more information in order to help me out!
Thank you
ImageRenderer.java
public class ImageRenderer implements Renderer {
...
private Context mActivityContext;
...
public SandAniRenderer(final Context activityContext) {
// TODO Auto-generated constructor stub
mActivityContext = activityContext;
mField = new Field(mActivityContext);
...
}
private boolean InitializeObject(int width, int height)
{
...
Field.SetField(width, height, R.drawable.image);
...
return true;
}
Field.java
public class Field extends xxx{
private final Context mActivityContext;
public Field(Context activityContext)
{
mActivityContext = activityContext;
}
public boolean SetField(int width, int height, int fn)
{
ImageView mImage = (ImageView) ((Activity) mActivityContext).findViewById(fn);
...
}
but it doesn't work!
mImage contains null image...
I can't find how can image contain outside of the activity.
Can you help me about how to implement drawing an ImageView outside of the activity?
Assuming mField is a member variable,
public class ImageRenderer implements Renderer {
Field mField = null;
use that in InitializeObject. Your Context is null because it wasn't set when you called it statically.
private boolean InitializeObject(int width, int height)
{
...
mField.SetField(width, height, R.drawable.image);
...
return true;
}
After you have set
mField = new Field(mActivityContext);
Try this (emptyimage is the image i have in my drawable)
mImage.setImageResource(R.drawable.emptyimage);
I'm trying to develop an Android 3.1 tablet application.
This app will have a lot of images, and I have followed Processing Bitmaps Off the UI Thread tutorial, but I've done something wrong because I get:
java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.Gallery$LayoutParams
This is my code:
I set up Gallery on an Activity
mFactGallery = (Gallery)mView.findViewById(R.id.factGallery);
mFactGallery.setAdapter(new ImageGalleryAdapter(mActivity, ImageView.ScaleType.FIT_END, 180, 90));
ImageGalleryAdapter.java
public class ImageGalleryAdapter extends BaseAdapter
{
private ArrayList<String> mImagesPath;
private Context mContext;
private int mWidth;
private int mHeight;
public ArrayList<String> getmImagesPath()
{
return mImagesPath;
}
public void setmImagesPath(ArrayList<String> mImagesPath)
{
this.mImagesPath = mImagesPath;
}
public void addImage(String imagePath)
{
mImagesPath.add(imagePath);
}
public ImageGalleryAdapter(Context context, ImageView.ScaleType scaleType, int width, int height)
{
mContext = context;
mWidth = width;
mHeight = height;
mScaleType = scaleType;
mImagesPath = new ArrayList<String>();
}
#Override
public int getCount()
{
return mImagesPath.size();
}
#Override
public Object getItem(int position)
{
return position;
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
// Get a View to display image data
ImageView imageView;
// if it's not recycled, initialize some attributes
if (convertView == null)
{
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
else
{
imageView = (ImageView) convertView;
// Recicla el Bitmap.
Bitmap bm = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
if (bm != null)
bm.recycle();
}
String filePath = mImagesPath.get(position);
if (BitmapTools.cancelPotentialWork(filePath, imageView))
{
String[] params = {filePath, Integer.toString(mWidth), Integer.toString(mHeight)};
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable =
new AsyncDrawable(mContext.getResources(), filePath, task);
imageView.setImageDrawable(asyncDrawable);
task.execute(params);
}
return imageView;
}
}
BitmapWorkerTask.java
public class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap>
{
private final WeakReference<ImageView> imageViewReference;
public String imgPath = "";
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
#Override
protected Bitmap doInBackground(String... params)
{
imgPath = params[0];
int width = Integer.valueOf(params[1]).intValue();
int height = Integer.valueOf(params[2]).intValue();
return BitmapTools.decodeSampledBitmapFromDisk(imgPath, width, height);
}
// Once complete, see if ImageView is still around and set bitmap.
#Override
protected void onPostExecute(Bitmap bitmap)
{
if (isCancelled())
{
bitmap = null;
}
if (imageViewReference != null && bitmap != null)
{
final ImageView imageView = imageViewReference.get();
final BitmapWorkerTask bitmapWorkerTask =
BitmapTools.getBitmapWorkerTask(imageView);
if (this == bitmapWorkerTask && imageView != null)
{
imageView.setImageBitmap(bitmap);
}
}
}
}
AsyncDrawable.java
public class AsyncDrawable extends BitmapDrawable
{
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
public AsyncDrawable(Resources res, String filepath,
BitmapWorkerTask bitmapWorkerTask)
{
super(res, filepath);
bitmapWorkerTaskReference =
new WeakReference<BitmapWorkerTask>(bitmapWorkerTask);
}
public BitmapWorkerTask getBitmapWorkerTask()
{
return bitmapWorkerTaskReference.get();
}
}
What am I doing wrong?
replace GridView.LayoutParams with Gallery.LayoutParams as follows, that will solve your problem
imageView.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
In ImageGalleryAdapter.java, this is wrong:
imageView.setLayoutParams(new GridView.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Use the correct LayoutParams class, it should be
imageView.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
(The superclass of LayoutParams is Gallery instead of GridView)