How to get Width and height of item in the drawable? - android

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)

Related

using javacamera2view in opencv

I am very new to both java and opencv especially android. From reading, I know that camera2 API support higher resolution compared to camera1 API, which is useful for my project.
I am trying to understand face-detection sample from open cv. I know that there are javacameraview and javacamera2view classes.
so far, I understand that the activity using javacameraview. however, I don't know where the javacameraview initially called. no decalaration at all.
I believe it is called in camerabridgeviewbase. but I cannot find any initialization of javacameraview.
The main goal is to replace javacameraview with javacamera2view. or use camera2 API directly.
I understand that there are some limitation in camera2 API but that is not the main problem now.
Here is the CameraBridgeViewBase initialisation
private static final String TAG = "CameraBridge";
protected static final int MAX_UNSPECIFIED = -1;
private static final int STOPPED = 0;
private static final int STARTED = 1;
private int mState = STOPPED;
private Bitmap mCacheBitmap;
private CvCameraViewListener2 mListener;
private boolean mSurfaceExist;
private final Object mSyncObject = new Object();
protected int mFrameWidth;
protected int mFrameHeight;
protected int mMaxHeight;
protected int mMaxWidth;
protected float mScale = 1;
protected int mPreviewFormat = RGBA;
protected int mCameraIndex = CAMERA_ID_ANY;
protected boolean mEnabled;
protected boolean mCameraPermissionGranted = false;
protected FpsMeter mFpsMeter = null;
public static final int CAMERA_ID_ANY = -1;
public static final int CAMERA_ID_BACK = 99;
public static final int CAMERA_ID_FRONT = 98;
public static final int RGBA = 1;
public static final int GRAY = 2;
cheers
Well iam looking in the wrong place. It is controlled in the layout. Just replace JavaCameraView with JavaCamera2View. And done. Camera2 implemented.

Android: Compute color dynamically

I have a Util class which is capable of creating color codes(decimal).
public class ColorUtils {
private static final String RED = "ff0000";
private static final String GREEN = "00ff00";
private static final String BLUE = "0000ff";
private static final String WHITE = "ffffff";
private static final int RADIX = 16;
public static int getColorShade(String deepShade, String lightShade, float percentage) {
if (percentage > 100) {
throw new RuntimeException("Percentage can not be more than 100");
}
int deepShadeCode = Integer.parseInt(deepShade, RADIX);
int lightShadeCode = Integer.parseInt(lightShade, RADIX);
int shadeDifference = deepShadeCode - lightShadeCode;
int shadeOffset = (int) (shadeDifference * percentage)/100;
return lightShadeCode + shadeOffset;
}
public static int getColorShade(String deepShade, float percentage) {
return getColorShade(deepShade, WHITE, percentage);
}
public static int getRedColorShade(float percentage) {
return getColorShade(RED, percentage);
}
public static int getGreenColorShade(float percentage) {
return getColorShade(GREEN, percentage);
}
public static int getBlueColorShade(float percentage) {
return getColorShade(BLUE, percentage);
}
public static int getWhite() {
return Integer.parseInt(WHITE, RADIX);
}
}
Is there any way to convert it to Android color?
All I could find out is ContextCompat.getColor(this,R.color.yourcolor)
but this will take resource id in second arguments, I don't want to make color pallets in color.xml. Is there a work around?
Don't know how you create the color, but if it can extract red, green, blue, then try this:
#ColorInt int color = Color.rgb(getRedColorShade(percentage), getGreenColorShade(percentage), getBlueColorShade(percentage));
Ref here, this time it's pretty sure that this method is added from API level 1 and can be used instead of valueOf

Make an custom view and extends it from other activities

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.

Android: how set image view outside of the activity

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);

Android XML annotation

I've been using Simple to try to read in my XML file to this class. I really don't know if I've annotated the classes correctly.
I don't know if I need this part:
public Frame()
{
super();
}
public Frame(int num, int x, int y, int width, int height,int offsetx,int offsety, int duration )
{
this.Num = num;
this.X = x;
this.Y = y;
this.Width = width;
this.Height = height;
this.OffsetX = offsetx;
this.OffsetY = offsety;
this.Duration = duration;]
What does super() do? Do I need getters/setters? Is what I added getters or setters? Do they call themselves automatically or what?
Here's the full class:
public class SpriteAnimationManag
{
// Animation frame class
#Element(name = "Frame")
public class Frame
{
#Element(name = "Num")
public int Num;
#Element(name = "X")
public int X;
#Element(name = "Y")
public int Y;
#Element(name = "Width")
public int Width;
#Element(name = "Height")
public int Height;
#Element(name = "OffSetX")
public int OffsetX;
#Element(name = "OffSetY")
public int OffsetY;
#Element(name = "Duration")
public float Duration;
public Frame()
{
super();
}
public Frame(int num, int x, int y, int width, int height,int offsetx,int offsety, int duration )
{
this.Num = num;
this.X = x;
this.Y = y;
this.Width = width;
this.Height = height;
this.OffsetX = offsetx;
this.OffsetY = offsety;
this.Duration = duration;
}
}
// Animaiton class to hold the name and frames
public class Animation
{
#Element(name = "Name")
public String Name;
#Element(name = "FrameRate")
public int FrameRate;//may need elementarray or list???
#Element(name = "Loop")
public boolean Loop;
#Element(name = "Pingpong")
public boolean Pingpong;
#ElementArray(name = "Frames")
public Frame[] Frames;
public Animation()
{
super();
}
public Animation(String name, int framerate, boolean loop, boolean pingpong, Frame[] frames)
{
this.Name = name;
this.FrameRate = framerate;
this.Loop = loop;
this.Pingpong = pingpong;
this.Frames = frames;
}
}
// The Sprite Texture stores the Sprite Sheet path.fr
public class SpriteTexture
{
// The Sprite Sheet texture file path
#Element(name = "path")
public String Path;
public SpriteTexture()
{
super();
}
public SpriteTexture(String path)
{
this.Path = path;
}
}
// Aniamtion Set contains the Sprite Texture and Animaitons.
#Root(name = "Animations")
public static class XNAAnimationSet
{
// The sprite texture object
#Element(name = "Texture")
public SpriteTexture SpriteTexture;
// The animation array in the Animation Set
#ElementArray(name = "Animation")
public Animation[] Animations;
public XNAAnimationSet()
{
super();
}
public XNAAnimationSet(SpriteTexture spritetexture, Animation[] animations)
{
this.SpriteTexture = spritetexture;
this.Animations = animations;
}
}
// Sprite Animation Manager class
public final static class SpriteAnimationManager
{
private static final String XNAAnimationSet = null;//was static private static
public static int AnimationCount;
// Read the Sprite Sheet Description information from the description xml file
public static XNAAnimationSet Read(String filename) throws Exception
{
XNAAnimationSet animationSet = new XNAAnimationSet();
Serializer serializer = new Persister();
try {
animationSet = serializer.read(XNAAnimationSet.class, filename );
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// Count the animations to Animation Count
AnimationCount = animationSet.Animations.length;
return animationSet;
}
}
}
I've been trying to see what's being read by trying to write the class to a file. The file is created but it's empty.
Can someone tell me if I've annotated this correctly? What am I doing wrong?
I was using jaxb there the last day to parse my xml, I'm not sure how similiar it is to the way your doing it but ill mention a few of the things i needed:
firstly, i think i needed a no-arg constructor in my class, which for you would just be -
public Frame(){};
I believe you do need getters, what you've got there arent getters/setters, your just declaring variables, this really is fundamental java stuff so it might be worth a read up on that before you continue.
When you have your getters defined properly, you then put the #XMLElement annotation above each of them, not above your variable declarators.
A getter looks like:
#XMLElement
public string getName(){ return this.Name};
Also id recommend trying to parse one class at a time, you have multiple inner classes here which i'd imagine gets messy when your trying to parse, i think you need to have #RootElement above the class name declarator, so the xml knows what type of object your creating.
Anyway, there's a few things off the top of my head, best of luck with it!

Categories

Resources