Android Change Image
I am trying to develop my first game but i am having trouble, it is something really simple which i am confused about. Basically I need to load 2 images i created in photoshop but if they are clicked, a different image of the same shape will be drawn on top, making it look like its been selected, then if someone clicks on the other image, image one goes back t开发者_如何学运维o the initial state and another image for the second one is drawn on top... How can I achieve this?
I am just having trouble on how to load the initial images to the screen and thenbe able to swap them with the others...
Thank you
If I understand your question right, it sounds like using tabs would be a solution. Smth like this might be a first step to your game
public class MyTab extends TabActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The acWednesdaytivity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, YourWithImage1.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("tab1").setIndicator("tab1",
res.getDrawable(R.drawable.ic_tab))
.setContent(intent);
tabHost.addTab(spec);
}}
and your ic_tab will look like
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey img-->
<item android:drawable="@drawable/ic_tab_grey"
android:state_selected="true" />
<!-- When not selected, use white img-->
<item android:drawable="@drawable/ic_tab_white" />
</selector>
I am new myself to andorid. Hope this will help somehow.
I'm new to Android too, so not sure if I have the correct answer. But here is what I might try:
- Have your activity implement onclicklistener
- In the onCreate method, find your ImageView using findViewById
- call the ImageView's setOnclickListener method
- Handle the onclick event
- In the event handler, call the setImageResource of the ImageView to switch the image.
Again, I'm new to Android too so I'm not talking from experience. I hope this helps.
See the following link which I happened to come across: http://www.higherpass.com/Android/Tutorials/Working-With-Images-In-Android/
精彩评论