2013-02-12 98 views
1

我是新來的android和製作幻燈片拼圖。我想通過在圖像瀏覽之間交換圖像而不是滑動圖像來保持簡單。 我的代碼doenst給出了任何錯誤,但也沒有做任何事情。 你能幫我嗎。如何在Android中的圖像瀏覽之間切換圖像?

這是我的XML代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="44dp" 
     android:layout_marginTop="44dp" 
     android:src="@drawable/ic_launcher" 
     android:tag="1" /> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageView1" 
     android:layout_marginLeft="3dp" 
     android:layout_toRightOf="@+id/imageView1" 
     android:src="@drawable/ic_launcher" 
     android:tag="2" /> 

    <ImageView 
     android:id="@+id/imageView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageView2" 
     android:layout_marginLeft="3dp" 
     android:layout_toRightOf="@+id/imageView2" 
     android:src="@drawable/ic_launcher" 
     android:tag="3" /> 

    <ImageView 
     android:id="@+id/imageView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/imageView1" 
     android:layout_below="@+id/imageView1" 
     android:layout_marginTop="3dp" 
     android:src="@drawable/ic_launcher" /> 

    <ImageView 
     android:id="@+id/imageView5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageView4" 
     android:layout_alignLeft="@+id/imageView2" 
     android:src="@drawable/ic_launcher" /> 

    <ImageView 
     android:id="@+id/imageView6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageView5" 
     android:layout_alignLeft="@+id/imageView3" 
     android:src="@drawable/ic_launcher" /> 

    <ImageView 
     android:id="@+id/imageView7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/imageView4" 
     android:layout_below="@+id/imageView4" 
     android:layout_marginTop="3dp" 
     android:src="@drawable/ic_launcher" /> 

    <ImageView 
     android:id="@+id/imageView8" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageView7" 
     android:layout_alignLeft="@+id/imageView5" 
     android:src="@drawable/ic_launcher" /> 

    <ImageView 
     android:id="@+id/imageView9" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageView8" 
     android:layout_alignRight="@+id/imageView6" 
     android:src="@drawable/ic_launcher" /> 

</RelativeLayout> 

,這是我的MainActivity代碼:

package com.example.puzzelencauseican; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.drawable.Drawable; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ImageView; 

public class MainActivity extends Activity implements OnClickListener { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ImageView image1=(ImageView) findViewById(R.id.imageView1); 
    Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.img1); 
    int width=(int)(bmp.getWidth() /2); 
    int height=(int)(bmp.getHeight() /2); 
    Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp,width, height,true); 
    image1.setImageBitmap(resizedbitmap); 
    image1.setOnClickListener(this); 

    ImageView image2=(ImageView) findViewById(R.id.imageView2); 
    Bitmap bmp2=BitmapFactory.decodeResource(getResources(), R.drawable.img2); 
    int width2=(int)(bmp2.getWidth() /2); 
    int height2=(int)(bmp2.getHeight() /2); 
    Bitmap resizedbitmap2=Bitmap.createScaledBitmap(bmp2, width2, height2,true); 
    image2.setImageBitmap(resizedbitmap2); 
    image2.setOnClickListener(this); 

    ImageView image3=(ImageView) findViewById(R.id.imageView3); 
    Bitmap bmp3=BitmapFactory.decodeResource(getResources(), R.drawable.img3); 
    int width3=(int)(bmp3.getWidth() /2); 
    int height3=(int)(bmp3.getHeight() /2); 
    Bitmap resizedbitmap3=Bitmap.createScaledBitmap(bmp3, width3, height3,true); 
    image3.setImageBitmap(resizedbitmap3); 
    image3.setOnClickListener(this); 

    ImageView image4=(ImageView) findViewById(R.id.imageView4); 
    Bitmap bmp4=BitmapFactory.decodeResource(getResources(), R.drawable.img4); 
    int width4=(int)(bmp4.getWidth() /2); 
    int height4=(int)(bmp4.getHeight() /2); 
    Bitmap resizedbitmap4=Bitmap.createScaledBitmap(bmp4, width4, height4,true); 
    image4.setImageBitmap(resizedbitmap4); 
    image4.setOnClickListener(this); 

    ImageView image5=(ImageView) findViewById(R.id.imageView5); 
    Bitmap bmp5=BitmapFactory.decodeResource(getResources(), R.drawable.img5); 
    int width5=(int)(bmp5.getWidth() /2); 
    int height5=(int)(bmp5.getHeight() /2); 
    Bitmap resizedbitmap5=Bitmap.createScaledBitmap(bmp5,width5, height5,true); 
    image5.setImageBitmap(resizedbitmap5); 
    image5.setOnClickListener(this); 

    ImageView image6=(ImageView) findViewById(R.id.imageView6); 
    Bitmap bmp6=BitmapFactory.decodeResource(getResources(), R.drawable.img6); 
    int width6=(int)(bmp6.getWidth() /2); 
    int height6=(int)(bmp6.getHeight() /2); 
    Bitmap resizedbitmap6=Bitmap.createScaledBitmap(bmp6,width6, height6,true); 
    image6.setImageBitmap(resizedbitmap6); 
    image6.setOnClickListener(this); 

    ImageView image7=(ImageView) findViewById(R.id.imageView7); 
    Bitmap bmp7=BitmapFactory.decodeResource(getResources(), R.drawable.img7); 
    int width7=(int)(bmp7.getWidth() /2); 
    int height7=(int)(bmp7.getHeight() /2); 
    Bitmap resizedbitmap7=Bitmap.createScaledBitmap(bmp7,width7, height7,true); 
    image7.setImageBitmap(resizedbitmap7); 
    image7.setOnClickListener(this); 

    ImageView image8=(ImageView) findViewById(R.id.imageView8); 
    Bitmap bmp8=BitmapFactory.decodeResource(getResources(), R.drawable.img8); 
    int width8=(int)(bmp8.getWidth() /2); 
    int height8=(int)(bmp8.getHeight() /2); 
    Bitmap resizedbitmap8=Bitmap.createScaledBitmap(bmp8,width8, height8,true); 
    image8.setImageBitmap(resizedbitmap8); 
    image8.setOnClickListener(this); 

    ImageView image9=(ImageView) findViewById(R.id.imageView9); 
    Bitmap bmp9=BitmapFactory.decodeResource(getResources(), R.drawable.leeg); 
    int width9=(int)(bmp8.getWidth() /2); 
    int height9=(int)(bmp8.getHeight() /2); 
    Bitmap resizedbitmap9=Bitmap.createScaledBitmap(bmp9,width9, height9,true); 
    image9.setImageBitmap(resizedbitmap9); 
    image9.setOnClickListener(this); 

} 

private void imageView1Click() { 
    ImageView image1=(ImageView) findViewById(R.id.imageView1); 
    ImageView image2 = (ImageView) findViewById(R.id.imageView2); 
    ImageView image4 = (ImageView) findViewById(R.id.imageView4); 
    Context mContext = null; 
    Drawable leeg; 

    leeg = mContext.getResources().getDrawable(R.drawable.leeg); 

    if(image4.getDrawable() == leeg){ 
     Drawable verander1 = image1.getBackground(); 
     Drawable verander2 = image4.getBackground(); 
     image4.setBackground(verander1); 
     image1.setBackground(verander2); 
    } 

    if (image2.getDrawable() == leeg){ 
     Drawable verander1 = image1.getBackground(); 
     Drawable verander2 = image2.getBackground(); 
     image2.setBackground(verander1); 
     image1.setBackground(verander2); 
    } 
} 



private void imageView8Click() { 
    ImageView image5=(ImageView) findViewById(R.id.imageView5); 
    ImageView image7 = (ImageView) findViewById(R.id.imageView7); 
    ImageView image8=(ImageView) findViewById(R.id.imageView8); 
    ImageView image9 = (ImageView) findViewById(R.id.imageView9); 
    Drawable leeg; 

    leeg = getResources().getDrawable(R.drawable.leeg); 

    if(image5.getDrawable() == leeg){ 
     Drawable verander1 = image8.getBackground(); 
     Drawable verander2 = image5.getBackground(); 
     image5.setImageDrawable(verander1); 
     image8.setImageDrawable(verander2); 
    } 

    if(image7.getDrawable() == leeg){ 
     Drawable verander1 = image8.getBackground(); 
     Drawable verander2 = image7.getBackground(); 
     image7.setImageDrawable(verander1); 
     image8.setImageDrawable(verander2); 
    } 

    if(image9.getDrawable() == leeg){ 
     Drawable verander1 = image8.getBackground(); 
     Drawable verander2 = image9.getBackground(); 
     image9.setImageDrawable(verander1); 
     image8.setImageDrawable(verander2); 
    } 

} 

public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.imageView1: 
     imageView1Click(); 
     break; 
    case R.id.imageView8: 
     imageView8Click(); 
     break; 
    } 
} 

回答

1

1)不要經常使用findViewById。使用一次並保存鏈接。這對性能操作來說相當複雜。

2)不要加載位圖兩次。您可以使用下面的方法通過一個操作創建兩倍縮放位圖。

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inSampleSize = 2; 
BitmapFactory.decodeResource(getResources(), R.id.myimage, options); 

3)

image4.getDrawable() == leeg 

將始終返回false這裏,因爲leeg是新創建的對象

不要比較喜歡==對象的內容。這個運算符適用於比較原始類型。 ==運算符將鏈接與對象進行比較,而不是對象的內容。您只會使用(object1 == object2)來檢查它是否指向同一個對象實例,而不是具有相同數據的對象。比較兩個可繪製(和字符串和等)在點擊方法調用imgView.getBackground並設置背景爲源動力的其他imageVIews使用equals()方法從Object類

if (image2.getDrawable().equals(leeg)) { 
+0

非常感謝你!我已經做出了改變,但仍然圖像不會改變,當我點擊它們。你知道它會是什麼嗎? 在此先感謝 – 2013-02-12 19:26:39

1

,就是你意?你不應該調用getDrawable嗎?

我建議你使用ViewPager來做這個功能,而不是使用所有的固定代碼。你也應該在onClick中評論代碼。

更新: 給這個佈局文件一試。我只是將它們包裝在Horizo​​ntalScrollView中,並修改了RelativeLayout的寬度。

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="44dp" 
     android:layout_marginTop="44dp" 
     android:src="@drawable/ic_launcher" 
     android:tag="1" /> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageView1" 
     android:layout_marginLeft="3dp" 
     android:layout_toRightOf="@+id/imageView1" 
     android:src="@drawable/ic_launcher" 
     android:tag="2" /> 

    <ImageView 
     android:id="@+id/imageView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageView2" 
     android:layout_marginLeft="3dp" 
     android:layout_toRightOf="@+id/imageView2" 
     android:src="@drawable/ic_launcher" 
     android:tag="3" /> 

    <ImageView 
     android:id="@+id/imageView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/imageView1" 
     android:layout_below="@+id/imageView1" 
     android:layout_marginTop="3dp" 
     android:src="@drawable/ic_launcher" /> 

    <ImageView 
     android:id="@+id/imageView5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageView4" 
     android:layout_alignLeft="@+id/imageView2" 
     android:src="@drawable/ic_launcher" /> 

    <ImageView 
     android:id="@+id/imageView6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageView5" 
     android:layout_alignLeft="@+id/imageView3" 
     android:src="@drawable/ic_launcher" /> 

    <ImageView 
     android:id="@+id/imageView7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/imageView4" 
     android:layout_below="@+id/imageView4" 
     android:layout_marginTop="3dp" 
     android:src="@drawable/ic_launcher" /> 

    <ImageView 
     android:id="@+id/imageView8" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageView7" 
     android:layout_alignLeft="@+id/imageView5" 
     android:src="@drawable/ic_launcher" /> 

    <ImageView 
     android:id="@+id/imageView9" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageView8" 
     android:layout_alignRight="@+id/imageView6" 
     android:src="@drawable/ic_launcher" /> 

</RelativeLayout> 

</HorizontalScrollView> 
+1

也許ViewFlipper?無法看到ViewPager適合的方式。 – 2013-02-12 18:58:33

+0

getDrawable ..我會查找它。我不知道多少機器人,所以我試圖讓它與我知道的事情合作謝謝你幫助 – 2013-02-12 19:31:17

+0

我不會去我一個很好的方向胡?你有沒有偶然有一個簡單的想法,應該如何編程一個幻燈片拼圖因爲我卡住了。任何代碼讓我開始將如此讚賞。 – 2013-02-12 19:37:38