I am trying to create a 2D avatar customizer without using a Game Engine inside my Android app.
I will basically have a PNG image as a base to start with. Then I want to layer other images on top of the base to customize the character.
What would be the best option to create this? I have already created a custom ImageView and overrided the onDraw():
public class AvatarView extends ImageView {
public AvatarView(Context context) {
super(context);
}
public AvatarView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AvatarView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.battle_run_char), 0, 70, null);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.red_cartoon_hat), 70, 0, null);
}
}
This seems very specific by using coordinates. Would there be a better way to achieve this without having to use coordinates?
EDIT:
Here is what I have so far. The guy is a separate image from the red hat.
If you are planning to add on that ImageView dynamically then there is not way to place those images without assigning pixel axis on it
one problem in your custom class is that dont ever decodeResource within your onDraw method that it will be called multiple times and it will cause a big lag problem, instead create an init method within your AvatarView and decode it, and call that init method in all of your constructor.
sample:
public class AvatarView extends ImageView {
private Bitmap body;
private Bitmap hat;
public AvatarView(Context context) {
super(context);
init();
}
public AvatarView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public AvatarView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init()
{
body = getResources(), R.drawable.battle_run_char);
hat = getResources(), R.drawable.red_cartoon_hat);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(BitmapFactory.decodeResource(body , 0, 70, null);
canvas.drawBitmap(BitmapFactory.decodeResource(hat , 70, 0, null);
}
}
Related
I want to load the url into ImageView and that url is dynamic, so I created the custom Imageview class like this
public class CustomLocalCurrencyImageViewWhiteColor extends AppCompatImageView {
public CustomLocalCurrencyImageViewWhiteColor(Context context) {
super(context);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
public CustomLocalCurrencyImageViewWhiteColor(Context context, AttributeSet attrs) {
super(context, attrs);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
public CustomLocalCurrencyImageViewWhiteColor(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
}
.Its working fine when I used it the following way
<com.kiran.example.ebitcoin.customview.CustomLocalCurrencyImageViewWhiteColor
android:id="#+id/img"
android:layout_width="#dimen/imageSizeTooSmallBTCLogo"
android:layout_height="#dimen/imageSizeTooSmallBTCLogo"
android:layout_gravity="center_vertical" />
Now, I want to change the image resource of this custom image too, dynamically, So I tried
CustomLocalCurrencyImageViewWhiteColor img = findViewById(R.id.img);
img.setImageResource(R.drawable.ic_bitcoin_24_white);
But unfortunately, nothing is changed. How to change the image of custom image view class dynamically?? Any one found answer of this?
Finally , I got answer of my own question. I follow this, and done the code by following way.
public class CustomLocalCurrencyImageViewWhiteColor extends AppCompatImageView {
Context context;
public CustomLocalCurrencyImageViewWhiteColor(Context context) {
super(context);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
public CustomLocalCurrencyImageViewWhiteColor(Context context, AttributeSet attrs) {
super(context, attrs);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
public CustomLocalCurrencyImageViewWhiteColor(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
public void setImageWithGlide(Context context, Integer resId) {
this.context = context;
Glide.with(context).load("").placeholder(resId).into(this);
}
}
To change the image dynamically,
CustomLocalCurrencyImageViewWhiteColor img = findViewById(R.id.img);
img.setImageWithGlide(this, R.drawable.image);
As you are extending AppCompatImageView you can do as following:
Drawable drawable = AppCompatDrawableManager.get().getDrawable(getApplicationContext(), R.drawable.ic_bitcoin_24_white);
img.setImageResource(drawable);
Well u already use Glide in the first place, so try do it this way:
create setImageWithGlide method in CustomLocalCurrencyImageViewWhiteColor
public void setImageWithGlide(Context context, String imagePath) {
Glide.with(context).load(imagePath).into(this)
}
public void setImageWithGlide(Context context, Integer resId) {
Glide.with(context).load(resId).into(this)
}
you can use it like this:
img.setImageWithGlide(imagePath);
or if u use resourceId
img.setImageWithGlide(R.drawable.image);
I am writing a custom view where I draw myself to the canvas in the onDraw(Canvas canvas) method.
My idea is to also animate the view as soon as it becomes visible by using an ObjectAnimator and a setter for a particular variable in my view. Class looks as follows:
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, -1);
}
public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public DrawView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr);
}
private void init(final Context context, AttributeSet attrs, int defStyleAttr) {
anim = ObjectAnimator.ofInt(this, PROGRESS, 0, 100);
anim.setDuration(animDuration);
if (getVisibility() == VISIBLE) {
getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
getViewTreeObserver().removeOnPreDrawListener(this);
animate();
return true;
}
});
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Draw something here using progress
}
protected void setProgress(final int pathProgressParam) {
// Re-draw
invalidate();
}
}
When triggering the animation, the "setProgress" method is called properly, and the View gets invalidated to force a re-draw with the new value for my progress field.
The problem comes when sometimes, let's say 30% of the times, the onDraw method is not called even though the View gets marked as "dirty", and only the very last frame is drawn, which kinda defeats the purpose of the animation.
I do not know whether there is any better step in the drawing cycle to trigger the animation, of if I should trigger it from somewhere else as well, but it seems as invalidate() is not enough. I have also looked at how Android guys perform property animations, but unfortunately the methods they used are hidden.
Btw, I have used the same idea as Romain Guy did here: https://github.com/romainguy/road-trip
i have the following code, and the problem, that my function rlposition() isn't available from outside the class.
public class RLbadge extends TextView {
public RLbadge(Context context) {
super(context);
this.setTypeface(null, Typeface.BOLD);
this.setTextColor(Color.WHITE);
this.setBackgroundResource(R.drawable.badge);
this.setTextSize(18);
}
public void rlposition(Button pButton) {
// THIS FUNCTION ISNT SEEN FROM OUTSIDE WHY?
}
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
}
}
Why is the function rlposition not visible from outside of the class?
Isn't it possible to add functionality to an extended TextView?
<YOURPACKAGENAME.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="TEASTING" />
Here is Class
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public MyTextView(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
// Do your staff
}
}
}
i now answered the question for myself and put it here maybe somebody needs the answer in the future.
The problem was this line of calling:
TextView badgeInfoscan = new RLbadge(this);
badgeInfoscan.rlposition(); // here the error comes
changed to
RLbadge badgeInfoscan = new RLbadge(this);
badgeInfoscan.rlposition(); // the function is visible
hi I am developing android application in which i am using one custom frame layout class. Inside that class I am using one drawable and with the help of canvas i m drawing that. I did this in following way :
public class BackgroundContainer extends FrameLayout implements OnTouchListener{
Drawable mShadowedBackground;
public BackgroundContainer(Context context) {
super(context);
init();
}
public BackgroundContainer(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BackgroundContainer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
mShadowedBackground =
getContext().getResources().getDrawable(R.drawable.actionbar);
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
Log.i("OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO", "OOOOOOOOOOOOOOOOOOOOOO");
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN: {
invalidate();
}
}
return true;
}
#Override
protected void onDraw(Canvas canvas) {
mShadowedBackground.setBounds(getWidth()-150, 0, getWidth(), mOpenAreaHeight);
canvas.save();
canvas.translate(0, mOpenAreaTop);
mShadowedBackground.draw(canvas);
canvas.restore();
}
}
}
Now I am want to listen click even on my drawable. I implement ontouch event but its not working. Am i doing it in wrong way. Need help thank you.
Drawables are not clickable as it is not considered as a view.
Like deepak already said: by implementing the corresponding listener you just provide the behavior what should happen for a specific event. You still need to add the listener :) In your case this would help (in your init()):
setOnTouchListener(this);
how can I find out when onDraw of a View has finished?
Thanks!
I don't know what you're ultimately trying to achieve, but if you need to run some logic once drawing has been completed in an Activity, then you can call View.post(Runnable) and put your logic inside there. onDraw would have likely taken place once the code in your Runnable has been reached since it put that Runnable on the message queue.
Place a boolean member in your View class and set it to true when onDraw is called (If you simply want to be able to test that a View has drawn).
Or if you want some sort of post-draw "event" call to execute a new thread- just put it at the end of onDraw.
Another suggestion how it could be done:
public class FVRTraceAbleListView extends ListView {
ListViewListener listener;
public interface ListViewListener {
void onPostDraw();
}
public FVRTraceAbleListView(Context context) {
super(context);
}
public FVRTraceAbleListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FVRTraceAbleListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public FVRTraceAbleListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public void setListener(ListViewListener listener) {
this.listener = listener;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (listener != null) {
listener.onPostDraw();
}
}
}