我是Android开发新手,我正在开发一个壁纸应用,我正在实施启动画面。
我想在显示时左右/左右移动闪屏。 请注意:我不是在谈论更改/滑动图像,我想移动显示图像。
请参阅下面的视频,它解释了我想要的内容:http://www.dailymotion.com/video/x2c8j8e_scr-20141210-183228_school
我不知道如何移动启动画面。所以请帮忙。
这是我的代码:
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.R;
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_1, R.drawable.splash_2, R.drawable.splash_3, R.drawable.splash_4, R.drawable.splash_5, R.drawable.splash_6,R.drawable.splash_7};
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);
}
}
答案 0 :(得分:0)
我认为您的意思是要加载壁纸图片然后自动滚动它。如果您还没有,则可以为扩展WallpaperService
的类找到多个模板。您还将看到使用Handler + Runnable在类中重复调用draw
的示例 - 这将为您提供动画。现在您需要做的就是使用类创建的画布的剪辑边界比屏幕尺寸更宽。像
Rect fullBoundsRect = c.getClipBounds();
fullBoundsRect.inset(-tilew, 0); //make the canvas bounds wider
会为你做到这一点。
然后你只需要绘制一个比屏幕边界大的位图(使用canvas.drawBitmap
)并使用Matrix
操作在屏幕上迭代移动它。
如果你使用一个大的位图,你将不得不小心地管理内存(你可能需要在裁剪的部分加载它 - drawBitmap
命令给你一些选项,在这种情况下你不会'需要加宽Canvas边界的扩展),但这给你基本的想法。
答案 1 :(得分:0)
您必须为背景设置动画。我们可以通过将图像移动到各自的方向来连续绘制画布和绘制图像。看到这篇博文,它可能会对你有帮助。
答案 2 :(得分:0)
<强> main.xml中强> `
<ImageView
android:id="@+id/img_animation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@null"
android:src="@drawable/wal" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="66dp"
android:text="Background Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/white" />
MainActivity.java
`public class MainActivity扩展了Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img_animation = (ImageView) findViewById(R.id.img_animation);
TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
0.0f, 0.0f);
animation.setDuration(5000);
animation.setRepeatCount(5);
// if you want infinite
// animation.setRepeatCount(Animation.INFINITE);
animation.setRepeatMode(2);
animation.setFillAfter(true);
img_animation.startAnimation(animation);
}
}`