web-dev-qa-db-ja.com

ImageViewで画像を点滅させる方法(Android)

スプラッシュ画面としてImageViewを使用して画像を表示している壁紙アプリに取り組んでいます。

ここで、ImageView画像をスプラッシュ画面として点滅させたいと思います。

これは私のスプラッシュ画面コードです:

import Java.util.ArrayList;
import Java.util.List;
import Java.util.Random;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Android.annotation.SuppressLint;
import Android.app.Activity;
import Android.app.AlertDialog;
import Android.content.DialogInterface;
import Android.content.Intent;
import Android.os.Bundle;
import Android.util.Log;
import Android.view.Window;
import Android.widget.ImageView;
import Android.widget.Toast;

import com.Android.volley.Request.Method;
import com.Android.volley.Response;
import com.Android.volley.VolleyError;
import com.Android.volley.toolbox.JsonObjectRequest;
import com.usd.amazingwallpapershd.app.AppConst;
import com.usd.amazingwallpapershd.app.AppController;
import com.usd.amazingwallpapershd.picasa.model.Category;

@SuppressLint("NewApi")
public class SplashActivity extends Activity {
    private static final String TAG = SplashActivity.class.getSimpleName();
    private static final String TAG_FEED = "feed", TAG_ENTRY = "entry",
            TAG_GPHOTO_ID = "gphoto$id", TAG_T = "$t",
            TAG_ALBUM_TITLE = "title";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        getActionBar().hide();
        setContentView(R.layout.activity_splash);

        // //splash images array
        // Randomise a background
        // int[] yourListOfImages = { R.drawable.splash_8, R.drawable.splash_8
        // };
        int[] yourListOfImages = { R.drawable.splash_1, R.drawable.splash_2,
                R.drawable.splash_3, R.drawable.splash_4 };

        Random random = new Random(System.currentTimeMillis());
        int posOfImage = random.nextInt(yourListOfImages.length - 1);

        ImageView imageView = (ImageView) findViewById(R.id.splash_imageview);
        imageView.setBackgroundResource(yourListOfImages[posOfImage]);
        // //

        // //

        // Picasa request to get list of albums
        String url = AppConst.URL_PICASA_ALBUMS
                .replace("_PICASA_USER_", AppController.getInstance()
                        .getPrefManger().getGoogleUserName());

        Log.d(TAG, "Albums request url: " + url);

        // Preparing volley's json object request
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url,
                null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, "Albums Response: " + response.toString());
                        List<Category> albums = new ArrayList<Category>();
                        try {
                            // Parsing the json response
                            JSONArray entry = response.getJSONObject(TAG_FEED)
                                    .getJSONArray(TAG_ENTRY);

                            // loop through albums nodes and add them to album
                            // list
                            for (int i = 0; i < entry.length(); i++) {
                                JSONObject albumObj = (JSONObject) entry.get(i);
                                // album id
                                String albumId = albumObj.getJSONObject(
                                        TAG_GPHOTO_ID).getString(TAG_T);

                                // album title
                                String albumTitle = albumObj.getJSONObject(
                                        TAG_ALBUM_TITLE).getString(TAG_T);

                                Category album = new Category();
                                album.setId(albumId);
                                album.setTitle(albumTitle);

                                // add album to list
                                albums.add(album);

                                Log.d(TAG, "Album Id: " + albumId
                                        + ", Album Title: " + albumTitle);
                            }

                            // Store albums in shared pref
                            AppController.getInstance().getPrefManger()
                                    .storeCategories(albums);

                            // String the main activity
                            Intent intent = new Intent(getApplicationContext(),
                                    MainActivity.class);
                            startActivity(intent);
                            // closing spalsh activity
                            finish();

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    getString(R.string.msg_unknown_error),
                                    Toast.LENGTH_LONG).show();
                        }

                    }
                }, new Response.ErrorListener() {

                    @SuppressWarnings("deprecation")
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e(TAG, "Volley Error: " + error.getMessage());

                        // show error toast
                        Toast.makeText(getApplicationContext(),
                                getString(R.string.splash_error),
                                Toast.LENGTH_LONG).show();

                        // Unable to fetch albums
                        // check for existing Albums data in Shared Preferences
                        if (AppController.getInstance().getPrefManger()
                                .getCategories() != null
                                && AppController.getInstance().getPrefManger()
                                        .getCategories().size() > 0) {
                            // String the main activity
                            Intent intent = new Intent(getApplicationContext(),
                                    MainActivity.class);
                            startActivity(intent);
                            // closing spalsh activity
                            finish();
                        } else {
                            // Albums data not present in the shared preferences
                            // Launch settings activity, so that user can modify
                            // the settings

                            // Intent i = new Intent(SplashActivity.this,
                            // SettingsActivity.class);
                            // // clear all the activities
                            // i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            // | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            // startActivity(i);

                            AlertDialog alertDialog = new AlertDialog.Builder(
                                    SplashActivity.this).create();
                            alertDialog
                                    .setMessage("Please connect to the internet and try again");
                            alertDialog.setButton("OK",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {
                                            finish();
                                        }
                                    });
                            alertDialog.show();

                        }

                    }
                });

        // disable the cache for this request, so that it always fetches updated
        // json
        jsonObjReq.setShouldCache(false);

        // Making the request
        AppController.getInstance().addToRequestQueue(jsonObjReq);

    }

}

これはBlinkActivityコードです:

import Android.app.Activity;
import Android.os.Bundle;
import Android.view.animation.Animation;
import Android.view.animation.Animation.AnimationListener;
import Android.view.animation.AnimationUtils;
import Android.widget.ImageView;

public class BlinkActivity extends Activity implements AnimationListener {

    ImageView img;

    // Animation
    Animation animBlink;

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

        img = (ImageView) findViewById(R.id.splash_imageview);

        // load the animation
        animBlink = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.blink);

        // set animation listener
        animBlink.setAnimationListener(this);

        // start the animation
        img.startAnimation(animBlink);

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        // Take any action after completing the animation

        // check for blink animation
        if (animation == animBlink) {
        }

    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }

    @Override
    public void onAnimationStart(Animation animation) {

    }

}

しかし、それを機能させるために両方を一緒に実装する方法がわかりませんか?

12
question

次のコードスニペットは、ImageViewを点滅させます。 ImageViewの背景を透明に設定します。

    Animation animation = new AlphaAnimation(1, 0); //to change visibility from visible to invisible 
    animation.setDuration(1000); //1 second duration for each animation cycle 
    animation.setInterpolator(new LinearInterpolator()); 
    animation.setRepeatCount(Animation.INFINITE); //repeating indefinitely 
    animation.setRepeatMode(Animation.REVERSE); //animation will start from end point once ended.
    imageButton.startAnimation(animation); //to start animation
36
Don Chakkappan

**このコードを試してください**

image = (ImageView) findViewById(R.id.image);
Animation animation = new AlphaAnimation((float) 0.5, 0); // Change alpha from fully visible to invisible
animation.setDuration(500); // duration - half a second
animation.setInterpolator(new LinearInterpolator()); // do not alter
                                                     // animation
                                                     // rate
animation.setRepeatCount(Animation.INFINITE); // Repeat animation
                                              // infinitely
animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the
                                            // end so the button will
                                            // fade back in
image.startAnimation(animation);
11
Jai Rajesh

ImageViewの背景を点滅させたくない場合(ソース画像のみを点滅させたくない場合)、またはそのコードを自分で記述したくない場合(これはまさに私が避けようとしていたことです)、次のようになります。カスタムビューを作成し、内部で実行します。
確かに、親FrameLayoutには回避策がありますが、実際に必要がない場合は、これ以上階層を作成しないでください。 :)

とにかく、私はそのような振る舞いを必要としていたので実際にBlinker Viewを書きました、そしてあなたはここからそれを自由に使うことができます: https://github.com/milosmns/blinking-image-view

ハッピーコーディング!

0
milosmns