Fit Width of Background Image But Keep Aspect Ratio in Android

April 5th, 2013 No comments

So you have an image that you want to use as a background on one of your activities.

android_01

 

You probably have something that looks like this.

android_scale_01

But the desired effect is to scale the original image into the blue outline below.

android_02

The actual effect (depending on your current layout)  is the graphic anchored to the top left (android:scaleType=”fitStart”) or center (android:scaleType=”fitCenter”) of the device’s screen with no scaling performed. Through all of your research on Google (directing you mostly to Stack Overflow threads) you’re stuck with a non-scaling graphic that’s positioned incorrectly on the screen. Or maybe you’ve got a distorted graphic that has stretched both in length and width and looks terrible. The short answer?

You can not scale the width of an ImageView to the screen’s resolution while maintaining aspect ratio in an Android layout using only XML.

That’s the information I got from one of the random Stack Overflows I found. And I ignored it for quite a while before giving in and trying to scale the image manually through code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
 
        <ImageView
            android:id="@+id/iv_background"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitCenter"
            android:src="@drawable/the_background" />
    </LinearLayout>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
 
        <Button
            android:id="@+id/b_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:text="@string/login_button" />
    </LinearLayout>
 
</RelativeLayout>

And the necessary code placed at the bottom of the activity’s onCreate method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
 
BitmapDrawable bmap = (BitmapDrawable) this.getResources().getDrawable(R.drawable.the_background);
float bmapWidth = bmap.getBitmap().getWidth();
float bmapHeight = bmap.getBitmap().getHeight();
 
float wRatio = width / bmapWidth;
float hRatio = height / bmapHeight;
 
float ratioMultiplier = wRatio;
// Untested conditional though I expect this might work for landscape mode
if (hRatio < wRatio) {
	ratioMultiplier = hRatio;
}
 
int newBmapWidth = (int) (bmapWidth*ratioMultiplier);
int newBmapHeight = (int) (bmapHeight*ratioMultiplier);
 
ImageView iView = (ImageView) findViewById(R.id.iv_background);
iView.setLayoutParams(new LinearLayout.LayoutParams(newBmapWidth, newBmapHeight));

android_scale_02

This results in an ImageView that’s stretched to fit the width of the screen while maintaining the aspect ratio of the image.

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks

Startup Weekend Postmortem: A Developers Perspective

March 22nd, 2013 1 comment

A couple weekends ago I took part in Startup Weekend Okanagan at the Kelowna Innovation Center. It’s a full, catered, weekend of building a product from scratch, intending to build a business around that product, and pitching it to a panel of judges. I was lucky enough to be one of 6 attendees from Vernon who were fully sponsored by way of entrance fee (thank you Vernon UPS Store & Bold Media) as well as 2 nights in a hotel (thank you City of Vernon). While it’s all still relatively fresh in my mind, I thought I’d share my thoughts on what went right, what went wrong, and what I’d do differently next time.

What Went Right

Similar but not-so-popular groups merged into one · Initially there were 12 groups who gathered enough votes to move on to the recruitment round (before the planning/development round ~ rest of the weekend). I was one of the 12 (lucky) but only had one person interested in helping me. After joining my team, and wandering the floor, he actually found other groups with little enough individual interest but with similar enough ideas that we all merged into one group. 12 groups boiled down to 7, and our group ended up with 9 members with well rounded skills.

Building an MVP (Minimum Viable Product) · You only have a single weekend to piece together the group’s collective ideas to form an end product. We built our product around WordPress, customizing it with pre-built plugins where necessary. We used WordPress to express how the product would work rather than wasting time building a functional prototype from the ground up. When the weekend is over, and presentations begin, you only have 5 minutes to pitch your business and product. At least half of which is a presentation on the business plan of your product. There’s not much time left to show off the actual product. So we took our customized WordPress site and made it look pretty with a dummy profile page, pre-filled meals and static calendar feature. Enough to be able to quickly click through the main features to help express to the judges how the product would be used.

Picking a product name early · As soon as the name was decided, the rest of the team (marketing/business) could get to work on the social branding pages, including Twitter and Facebook. A logo was born, promotional video was shot. The team really felt like they had something to build around.

Ignoring everyone while they picked a name · From a developer perspective, work had to get done. We already knew we were building around WordPress but had no idea what the url of the site would be. So I set to work configuring my hosts file and apache vhosts with a dummy domain, installing WordPress, and starting on my piece of the project.

Focus on a limited feature set · We were 5 teams that merged together. 3 of our individual team ideas were very similar. We wanted to provide a service around sharing specific items. “Professional skills” was one idea, I wanted to do a co-operative advertisement service. The other idea was to share meals. Obviously cooking is a very specific “professional skill” which we could use to drive focus toward our product. We could have chosen lawn mowing. We could have chosen painting. We could have tried to tackle “all odd jobs” but we focused on this vertical slice, limiting the features and helping us make the most of the limited time we had.

What Went Wrong

I did not prepare my pitch · You get 60 seconds to pitch your idea. They are strict when it comes to those 60 seconds. I was just about to get to the good part when the buzzer rang and I was ushered off the floor. Without maximizing the use of my 60 seconds I failed to gain enough interest from my peers and barely gained enough votes to bring my idea it into the team building round.

Spending time creating features never shown · As previously stated, your time is limited. Part of my responsibilities early in the weekend were to work on the customer-facing interface. This had me researching how to customize the WordPress user profile page and installing and configuring the Adminimize plugin to limit what dashboard features the non-admin users would get to see. This was time wasted since during the final presentation we were never going to walk through the process of 1) signing up, 2) creating meals, and 3) “purchasing” other people’s meals.

Picking a product name early · You’ll notice this is in “what went right” as well. Most of the team was involved in this, while myself and the other programmer tried our best to start building something without first knowing the brand. It seemed like a whole hour (of our precious 50 or so) was dedicated to what we should call ourselves. It’s a necessary activity but most of the team was doing nothing else but this for the hour.

What I’d Do Differently

I’m guilty of this myself in that I did not do this, and had the project I’d pitched had support behind it I’d have wasted time, too: Already have a list of potential product names before the group meets for the first time. Having the brand figured out before-hand will help everyone hit the ground running. The community/social brand manager could then get to work right away creating the social profiles used to generate buzz. The project manager can get started securing a domain which in turn allows the developers to set up the site and their workspaces.

Next time I will not eat so much. There’s more food than anyone can eat in a weekend. Drinks included. I brought my own “snacks” (chocolate covered coffee beans and chocolate covered raisins) and I barely had an opportunity to eat them. I spent the weekend half focused on how uncomfortable I was from all of the food and drink in me. My own fault, a lesson learned.

While I didn’t see it as an issue, the other developer had said he wished everyone communicated a little better. I can see where he’s coming from but I was actually impressed with how well everyone worked together. Given that he thought that was an area for improvement, imagine how much we’d have accomplished if we did all communicate a little more. We didn’t really have an acting “project manager”. Rather, we had “the guy whose idea we were working on” and we all approached him when we had a specific question. If I’m responsible for that role next time, taking this lesson learned, I’ll make an effort to circle the room every so often to ensure 1) no one is working on duplicate tasks unless required and 2) there are no unasked questions from those who may not like to ask questions. All in all, we as a team didn’t suffer from 1) or 2) but a little more communication may have led to a better finished product.

Conclusion

Startup Weekend is a fantastic experience. Not only do you meet many people, making it a great networking event, but you’ll learn a thing or two as well. I know of an applications developer who learned MySQL for the first time. A Joomla developer who’s now eager to try his hand with WordPress. Myself, I was ready to build the MVP from scratch, account management system and all, before someone suggested we use WordPress instead. Brilliant.

Go for the comforts of food and drink and the company of like-minded individuals. Stay for the challenge. Take away a wealth of knowledge and industry contacts. And just maybe you’ll take first place.

*Note: We didn’t take first place. Our project, ChefShuffle.com, took the “biggest social impact” award. Coo Coo, a parking space service, took runner-up. Arkitektor took first place.

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks

Squishy Bugs Gameplay

January 11th, 2013 No comments

I finally figured out how to record video on my computer (no audio). So here’s a session of Squishy Bugs, in case you’ve wanted to see it in action before downloading and playing this relatively small in file-size and free game.

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
Categories: Games Tags: , , ,

Inspiration Motivation Perspiration Activation

January 5th, 2013 No comments

[Edit: Tommy of Team Meat said this all a whole lot better, in greater detail]

[Edit again: Neil Gaiman also says it better in a speech]

[Edit 3: While I'm making edits, I may as well add this one. I always go back to this one every half a year or so. Here's Gary Vaynerchuk. It's less in line with the other edits, and the message below, but I still find it very motivational]

I just went on a bit of a mini motivational rant on Twitter. There should be 6 links in that previous sentence there.

So the context of the rant? Basically if you want to do something, just do it. Don’t worry about the right way to do it. There is no right way. There is only your way.

I speak from the vantage point of a programmer/software developer, so I’m referring to source code when I say there is no right way to do something. Don’t let your inexperience stop you from trying. You’ll write sloppy code. You’ll paint sloppy pictures. You’ll write sloppy stories. Whatever it is you aspire to do, do it, and expect to do it poorly to start.

My biggest mental block when it came to game development was that I didn’t want to really do it until I knew how to do it the right way. And that cost me years. I got all hung up that it wasn’t worth doing unless I could do it right. Don’t worry about the right way. Just do it your way. You’ll get better, you’ll stop wasting time studying and reading and theorizing.

Practice makes perfect.

Just my rambling 2 cents. I hope that after you read this you’re motivated or inspired because it’s really not worth sitting around waiting for the right moment where you’ll suddenly and magically be good enough to do things the right way.

[Edit 4: An excellent in-line response to a HackerNews item]

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
Categories: Life Tags:

Squishy Bugs Beta

December 23rd, 2012 No comments

December 24 (12 hours or so from now) you can get your hands on the Squishy Bugs beta. I’m presently testing the beta making sure all of the features are in place and that everything is working. It has been a long time in the making. 1 year ago tomorrow I released the very first version of the game. It wasn’t pretty and I probably “shot myself in the foot” by releasing the game in such an early unfinished state. If I can find some screenshots I’ll edit this post and put them below this paragraph.

Early Title Screen Early Game

Shoot forward ~365 days, now I have no problem calling the game done. You’ll get to play the beta, I’ll continue testing for a few days after the beta release, then I’ll release version 1.0 on my birthday! I turn 29 on December 29.

Squishy Bugs Tweet

That image above is the “big feature” of this next update. You can now share your high scores to Twitter and Facebook directly from within the game. I wouldn’t call coding that feature “game development” but I think it’s a necessary addition to the game. There was a lot of programming external to the actual game that was required in making Squishy Bugs.

The biggest Squishy Bugs related project being the WaggSoft api and accompanying Squishy Bugs high score list. Developing those server side features was a lot of work but the bright side is those features will be used in my future games. I don’t need to code them again. They’re done, which will speed up development of my next game(s).

So please, if you have an Android device, give Squishy Bugs a try. It’s free. It’s ad-supported. I make pennies from it per month so I’m not getting rich off this. Tell your friends. When the beta and version 1 are released please make use of the Twitter and/or Facebook sharing feature at least once.

If you have any questions you can reach me on my personal Twitter or the WaggSoft Twitter. If you’re press, (kyle at) shoot me an email (n3wt0n.com) or check out the Squishy Bugs presskit(). There are also the WaggSoft Forums.

Happy holidays to you all! Thanks for playing.

-Kyle D. Newton

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
Categories: Game Dev Tags: , , , ,

Switch to our mobile site