我是初学者,所以请多多包涵。我创建了一个应用程序,允许您滚动浏览一系列壁纸并选择一个。一切都很完美,但android只将左下角设置为壁纸......壁纸尺寸为640 x 960。
这是我的代码:
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class Wallpapers extends Activity implements OnClickListener{
Button preview;
Button setBackground;
Button returnBack;
ImageView display;
ImageView wallpaper1;
ImageView wallpaper2;
ImageView wallpaper3;
ImageView wallpaper4;
LinearLayout backgroundPreview;
private int image = R.drawable.wallpaper1;
HorizontalScrollView scrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpapers);
preview= (Button)findViewById(R.id.wallpaperPreview);
setBackground = (Button)findViewById(R.id.wallpaperSet);
returnBack = (Button)findViewById(R.id.wallpaperReturn);
preview.setOnClickListener(this);
setBackground.setOnClickListener(this);
returnBack.setOnClickListener(this);
display=(ImageView)findViewById(R.id.wallpaperDisplay);
wallpaper1=(ImageView)findViewById(R.id.wallpaper1);
wallpaper2=(ImageView)findViewById(R.id.wallpaper2);
wallpaper3=(ImageView)findViewById(R.id.wallpaper3);
wallpaper4=(ImageView)findViewById(R.id.wallpaper4);
wallpaper1.setOnClickListener(this);
wallpaper2.setOnClickListener(this);
wallpaper3.setOnClickListener(this);
wallpaper4.setOnClickListener(this);
backgroundPreview = (LinearLayout)findViewById(R.id.wallpaperActivityLayout);
scrollView = (HorizontalScrollView)findViewById(R.id.horizontalScrollView);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.wallpaperPreview:
previewBackground(image);
break;
case R.id.wallpaperSet:
new AlertDialog.Builder(this)
.setMessage("Would you like to set this image as your wallpaper?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
InputStream is = getResources().openRawResource(image);
Bitmap wallpaper = BitmapFactory.decodeStream(is);
try{
getApplicationContext().setWallpaper(wallpaper);
}catch(IOException e){
e.printStackTrace();
}
}
})
.setNegativeButton("No", null)
.show();
break;
case R.id.wallpaperReturn:
reverse();
break;
case R.id.wallpaper1:
display.setImageResource(R.drawable.wallpaper1);
image = R.drawable.wallpaper1;
break;
case R.id.wallpaper2:
display.setImageResource(R.drawable.wallpaper2);
image = R.drawable.wallpaper2;
break;
case R.id.wallpaper3:
display.setImageResource(R.drawable.wallpaper3);
image = R.drawable.wallpaper3;
break;
case R.id.wallpaper4:
display.setImageResource(R.drawable.wallpaper4);
image = R.drawable.wallpaper4;
break;
}
}
private void previewBackground(int image) {
backgroundPreview.setBackgroundResource(image);
setBackground.setVisibility(View.GONE);
preview.setVisibility(View.GONE);
display.setVisibility(View.GONE);
scrollView.setVisibility(View.GONE);
returnBack.setVisibility(View.VISIBLE);
}
private void reverse() {
backgroundPreview.setBackgroundResource(android.R.color.black);
setBackground.setVisibility(View.VISIBLE);
preview.setVisibility(View.VISIBLE);
display.setVisibility(View.VISIBLE);
scrollView.setVisibility(View.VISIBLE);
returnBack.setVisibility(View.GONE);
}
}