设置壁纸应用程序android eclipse

时间:2015-11-17 14:25:19

标签: android wallpaper

我开发了一个关于日食的应用程序来设置手机的壁纸。应用程序工作正常,但壁纸模糊。它是我项目中不可或缺的一部分。如果不解决这个问题,我无法继续进一步。

这是代码

    MainActivity.java
    public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button button =(Button) findViewById(R.id.button1);

            button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    startActivity(new Intent("com.example.backgroundapp.bakapp"));
                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

    }
The backapp java file
{
    bakapp.java
    public class bakapp extends Activity implements OnClickListener {
        ImageView display;
        int tophone;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);

            // to our activity to cover the whole screen
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);

            setContentView(R.layout.back);
            Button but = (Button) findViewById(R.id.setwallpaper);
            tophone = R.drawable.image4;
            but.setOnClickListener(this);
            display = (ImageView) findViewById(R.id.ivdisplay);
            ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
            ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
            ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
            ImageView image4 = (ImageView) findViewById(R.id.IVimage4);
            ImageView image5 = (ImageView) findViewById(R.id.IVimage5);

            image1.setOnClickListener(this);
            image2.setOnClickListener(this);
            image3.setOnClickListener(this);
            image4.setOnClickListener(this);
            image5.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast var;
            switch (v.getId()) {
            case R.id.IVimage1:
                display.setImageResource(R.drawable.n1);
                var = Toast.makeText(bakapp.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.n1;
                break;
            case R.id.IVimage2:
                display.setImageResource(R.drawable.n2);
                var = Toast.makeText(bakapp.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.n3;
                break;
            case R.id.IVimage3:
                display.setImageResource(R.drawable.n4);
                var = Toast.makeText(bakapp.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.n4;
                break;
            case R.id.IVimage4:
                display.setImageResource(R.drawable.n5);
                var = Toast.makeText(bakapp.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.n5;
                break;
            case R.id.IVimage5:
                display.setImageResource(R.drawable.n6);
                var = Toast.makeText(bakapp.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.n6;
                break;
            case R.id.setwallpaper:
                // to set a background we need to use bitmap
                InputStream is = getResources().openRawResource(tophone);
                // to phone is a variable that is updated everytime we click on an
                // ImageView to that imageview resource and by clicking the button
                // we set the phone background to that image.
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                Bitmap bm = BitmapFactory.decodeStream(is,null,options);
                // decode inputstream is
                try {
                    getApplicationContext().setWallpaper(bm);
                    // to set the wallpaper of the phone background we need to ask
                    // permission from the user so add permission of background from
                    // manifest file

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                var = Toast.makeText(bakapp.this, "Wallpaper image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                break;
            }
        }

    }
The layouts are as follows
    main.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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Application to Change background picture of device"
            android:textSize="30dp" />

        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="96dp"
            android:text="Click to launch" />

    </RelativeLayout>

    Layout for bakapp.java
    back.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/ivdisplay"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center"
            android:src="@drawable/image4" />
    "
        <Button
            android:id="@+id/setwallpaper"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="set image background" >
        </Button>

        <HorizontalScrollView
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/IVimage1"
                    android:layout_width="125dp"
                    android:layout_height="125dp"
                    android:padding="15dp"
                    android:src="@drawable/n1" />

                <ImageView
                    android:id="@+id/IVimage2"
                    android:layout_width="125dp"
                    android:layout_height="125dp"
                    android:padding="15dp"
                    android:src="@drawable/n2" />

                <ImageView
                    android:id="@+id/IVimage3"
                    android:layout_width="125dp"
                    android:layout_height="125dp"
                    android:padding="15dp"
                    android:src="@drawable/n3" />

                <ImageView
                    android:id="@+id/IVimage4"
                    android:layout_width="125dp"
                    android:layout_height="125dp"
                    android:padding="15dp"
                    android:src="@drawable/n4" />

                <ImageView
                    android:id="@+id/IVimage5"
                    android:layout_width="125dp"
                    android:layout_height="125dp"
                    android:padding="15dp"
                    android:src="@drawable/n5" />
            </LinearLayout>
        </HorizontalScrollView>

    </LinearLayout>

0 个答案:

没有答案