LiveWallpaper背景

时间:2013-10-28 13:25:02

标签: android live-wallpaper

我在预览模式下有动态壁纸的问题。壁纸图像不适合横向模式。它在肖像上很有效。我需要你的帮助。你会帮助我。我已经添加了代码

RajawaliRenderer.java

public class RipplesRenderer extends RajawaliRenderer {
    private final int NUM_CUBES_H = 4;
    private final int NUM_CUBES_V = 4;
    private final int NUM_CUBES = NUM_CUBES_H * NUM_CUBES_V;
    //private Animation3D[] mAnims;
    private TouchRippleFilter mFilter;
    private long frameCount;
    private final int QUAD_SEGMENTS = 40;
    int mScreenHeight,
    mScreenWeight;
    Gallery_Activity mm;
    boolean flag_check = false;
    int pos = 0;
    int viewBackgroundImageName;
    Bitmap texture;
    SimpleMaterial planeMat;
    int randPosition=1;

    int change_value;
    int Ripple_number,speed1;
    Preferences preferences;

    private MediaPlayer myplayer;
    private boolean sound;

    public RipplesRenderer(Context context) {
        super(context);
        setFrameRate(50);
        this.mContext=context;

        randPosition = BitmapUpdate.randomGenerator;
        texture = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.gthree_one);
        //sp=mContext.getSharedPreferences("wallpapersettings",0);
        preferences=new Preferences(mContext);
    }

    protected void initScene() {

        if(randPosition!=1) {
            texture = BitmapUpdate.bit;
        }

        mCamera.setPosition(0, 0, -9);

        DirectionalLight light = new DirectionalLight(0, 0, 1);
        light.setPower(1f);

        BaseObject3D group = new BaseObject3D();
        DiffuseMaterial material = new DiffuseMaterial();
        material.setUseColor(true);
        mScreenHeight = GNWallpaper.hieght;

        mScreenWeight = GNWallpaper.weight;
        Random rnd = new Random();        

        planeMat = new SimpleMaterial();
        Plane plane=new Plane(9,7,1,1);

        //Plane plane = new Plane(4, 4, 1, 1);
        plane.setRotZ(-90);
        plane.setScale(1.0f);
        plane.setMaterial(planeMat);
        addChild(plane);

        mFilter = new TouchRippleFilter();

        mPostProcessingRenderer.setQuadSegments(QUAD_SEGMENTS);
        mPostProcessingRenderer.setQuality(PostProcessingQuality.MEDIUM);
        addPostProcessingFilter(mFilter);
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        if (pos != com.themebowlapp.galaxynote3livewallpaper.Gallery_Activity.wallpaper_position || randPosition != BitmapUpdate.randomGenerator) {
            texture = BitmapUpdate.bit;
            pos = com.themebowlapp.galaxynote3livewallpaper.Gallery_Activity.wallpaper_position;
            randPosition = BitmapUpdate.randomGenerator;
        }

        Ripple_number=preferences.getSpeed_controler();

        speed1=Ripple_number*100;
        super.onSurfaceCreated(gl, config);
    }

    public void onDrawFrame(GL10 glUnused) {
        super.onDrawFrame(glUnused);

        mFilter.setTime((float) frameCount++ *.05f);
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        super.onSurfaceChanged(gl, width, height);
        mFilter.setScreenSize(width, height);
        mFilter.setRippleSize((40+speed1));
        planeMat.addTexture(mTextureManager.addTexture(texture));
    }

    public void setTouch(float x, float y) {
        mFilter.addTouch(x, y, frameCount *.05f);
    }

    @Override
    public void  onTouchEvent(MotionEvent event) {
        final int action = event.getAction();

        if(event.getAction() == MotionEvent.ACTION_DOWN) {            
            //sound
            myplayer = MediaPlayer.create(getContext(), R.raw.water_drop);
            myplayer.setVolume(100, 100);
            myplayer.start();

            myplayer.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer myplayer) {
                    myplayer.release();
                } 
            });

            setTouch(event.getX() / mScreenWeight, 1.0f - (event.getY() / mScreenHeight));
        }

        super.onTouchEvent(event);
    }
}

Settings.java

public class Settings extends Activity  {
    public TextView SettingTextObj, BackgroundTextObj;
    private RelativeLayout chose_background;
    public Preferences preferences;
    Context cont = this;
    private CheckBox soundcheckbox;
    private String PREFRENCES_NAME;
    SharedPreferences settings;
    //    private Button choosebackground;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        chose_background = (RelativeLayout) findViewById(R.id.BackgroundLayoutId);
        BackgroundTextObj = (TextView) findViewById(R.id.backgroundTxtViewId);

        soundcheckbox = (CheckBox)findViewById(R.id.checkBox1);
        settings = getSharedPreferences(PREFRENCES_NAME, 0);
        Boolean isChecked = settings.getBoolean("cbx1_ischecked", false);
        soundcheckbox.setChecked(isChecked);

        soundcheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            private MediaPlayer myplayer;
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
                Editor editor = getSharedPreferences(PREFRENCES_NAME, 0).edit();
                editor.putBoolean("cbx1_ischecked", isChecked);
                editor.commit();
                Toast.makeText(getApplicationContext(), "Check", Toast.LENGTH_SHORT).show();
                myplayer = MediaPlayer.create(getBaseContext(), R.raw.water_drop);
                myplayer.setVolume(100, 100);
                myplayer.start();
            }
        });


        preferences = new Preferences(cont);

        chose_background.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] items = { "Phone Gallery", "Choose Background" };

                AlertDialog.Builder builder = new AlertDialog.Builder(
                        Settings.this);
                builder.setTitle("Pick a Background");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        if (items[item].equals(items[0])) {
                            startActivity(new Intent(Settings.this, PhoneGallery_Activity.class));
                        } else {
                            startActivity(new Intent(Settings.this, Gallery_Activity.class));
                        }
                    }
                });

                AlertDialog alert = builder.create();
                alert.show();
            }
        });
    }

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

GNWallpaper.java

public class GNWallpaper extends Wallpaper {
    private RipplesRenderer mRenderer;
    private  String imageBg;
    private int cvwidth;
    private int cvheight;
    private int visibleWidth;
    private Bitmap bg;
    private String LoadText;
    private boolean sound;
    private MediaPlayer myplayer;
    public static WindowManager display;
    static int height,width;

    //private Integer[] mImageIds = { R.drawable.gthree_one,R.drawable.gthree_two,R.drawable.gthree_three, R.drawable.gthree_four, R.drawable.gthree_five, R.drawable.gthree_six,};
    private int position;

    public Engine onCreateEngine() {
        display =(WindowManager) getSystemService(Context.WINDOW_SERVICE);

        height= display.getDefaultDisplay().getHeight();
        width= display.getDefaultDisplay().getWidth();
        mRenderer = new RipplesRenderer(this);

        //Log.i("shibbu"," hello");
        return new WallpaperEngine(this.getSharedPreferences(SHARED_PREFS_NAME,
                Context.MODE_PRIVATE), getBaseContext(), mRenderer, false);
    }

    public void onSharedPreferenceChanged(SharedPreferences prefs,
            String key) {

        imageBg = prefs.getString("image_custom", "Bad Image");
        getBackground();

        sound=prefs.getBoolean("pref_sound", false);

        //          //sound
        //            sound=prefs.getBoolean("pref_sound", false);
    }

    void getBackground() {
        if (this.cvwidth == 0 || this.cvheight == 0 || this.visibleWidth == 0) {
            this.cvwidth = 1290;
            this.cvheight = 800;
            this.visibleWidth = 1290;
        }

        if(new File(imageBg).exists()) {
            int SampleSize = 1;

            do {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                bg = BitmapFactory.decodeFile(imageBg, options);
                SampleSize = (int) (Math.ceil(options.outWidth/(this.visibleWidth * 2))*2);
                options.inJustDecodeBounds = false;
                try {
                    options.inSampleSize = SampleSize;
                    bg = BitmapFactory.decodeFile(imageBg, options);
                } catch (OutOfMemoryError e) {
                    SampleSize = SampleSize * 2;
                }
            } while (bg == null);

            bg = Bitmap.createScaledBitmap(bg, this.cvwidth/2, this.cvheight, true);
        } else {
            bg = BitmapFactory.decodeResource(getResources(), R.drawable.gthree_one);
            //bg = BitmapFactory.decodeResource(getResources(), mImageIds[position]);
            //position++;
            bg = Bitmap.createScaledBitmap(bg, this.cvwidth/2, this.cvheight, true);
            LoadText = "";    
        }
    }
}

1 个答案:

答案 0 :(得分:0)

参考此链接。它将帮助您更改为横向视图.. http://tips4android.blogspot.in/2012/01/android-tips-how-to-get-screen.html