Frosty glass background effect in android activity - java

I am trying to apply a frosty glass effect to the background image of an activity, but I don't know how to do this can anyone know or do this kind of application if yes then help me please.
Expected output:-
This is my code where I am using Blurry library but when the application run it does work as expected:-
public class SplashActivity extends AppCompatActivity {
long delay = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_activity);
Blurry.with(getApplicationContext())
.radius(25)
.sampling(2)
.async()
.animate(500)
.capture(findViewById(R.id.content))
.into((ImageView) findViewById(R.id.content));
Timer runSplash = new Timer();
TimerTask showSplash = new TimerTask() {
#Override
public void run() {
finish();
startActivity(new Intent(SplashActivity.this,MainActivity.class));
}
};
runSplash.schedule(showSplash,delay);
}
}

May be this library is helpful to you.
https://android-arsenal.com/details/1/2192

Related

Android: Media Player not pausing in second activity using ImageView

I have spent almost the day trying to figure it out and yet could not manage to achieve it. I basically have two activities, MainActivity.java and GameActivity.java. The music stops but after going back to MainActivity.java and coming back to GameActivity.java using ImageView several times.
MainActivity.java
public class MainActivity extends AppCompatActivity {
ImageView iv;
public static final String BG_SOUND_CHECK = "background playing!";
// public static MediaPlayer backgroundSound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler handler = new Handler();
final MediaPlayer backgroundSound = MediaPlayer.create(this, R.raw.background_music);
handler.postDelayed(new Runnable() {
#Override
public void run() {
backgroundSound.setLooping(true);
backgroundSound.start();
Log.v(BG_SOUND_CHECK, "After loop Started!");
}
}, 3000);
getSupportActionBar().hide();
iv = (ImageView)findViewById(R.id.playImage);
iv.setOnClickListener(new View.OnClickListener(){
public void onClick(View view1){
// backgroundSound.setLooping(false);
backgroundSound.pause();
// Log.v(BG_SOUND_CHECK, "Playing After Play Button Click");
Intent myIntent = new Intent(MainActivity.this, GameActivity.class);
// myIntent.putExtra("backgroundSoundObj",backgroundSound);
startActivity(myIntent);
// Log.v(BG_SOUND_CHECK, "After Game Scene");
}
});
}
}
GameActivity.java
public class GameActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
// MainActivity.backgroundSound.pause();
// Rest of the code
}
}
I tried putting the music onPause(); in
1) ImageView onClick();
2) As soon as the GameActivity.java is loaded
but both ways end up pausing it only after I try coming back to MainActivity.java and going to GameActivity.java several times.
Do you see how you put your backgroundSound.start() inside the postDelayed-3000 ?
backgroundSound.pause() anytime before that 3000ms is not going to do anything.
It has nothing to do with going back and forth between your two activities, it just so happens that doing that takes up more than 3000ms, so when you do click for pause(), 3000ms has passed.
What you can do, to fit your current code, is to separate that into a Runnable.
Runnable bgSound = new Runnable() {
#Override
public void run() {
backgroundSound.setLooping(true);
backgroundSound.start();
Log.v(BG_SOUND_CHECK, "After loop Started!");
}
}
Use it with handler.postDelayed(bgSound, 3000)
In your onClick, if you want to stop it from playing before it starts, can use handler.removeCallbacks(bgSound), to stop it after it plays, your pause(). If you don't care to know if it has or hasn't started, you can do both alongside each other, should work fine too.
(if you don't want the music to play after you've left your main activity, you should also do the same actions in your onDestroy and/or onPause)

Why doesnt ImageView work for me? Android

My code is the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_game);
//initialize arks
new Timer().schedule(new TimerTask() {
#Override
public void run() {
initializeArks();
}
}, 2000);
}
Nothing special there, here comes the initializeArks():
public void initializeArks(){
iv= (ImageView) findViewById(R.id.imageView);
tw= (TypeWriter) findViewById(R.id.textView);
CountDownTimer timer = new CountDownTimer(10, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
iv.setImageResource(R.drawable.loading);
}
}.start();
}
Keep in mind that this code is only for debugging, it doesnt do anything specific, but as I found out the problem is with the ImageView
Also i have my variables declared STATIC for the purpose of using them in my other classes:
public static ImageView iv;
public static TypeWriter tw;
My XML is a basic one:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
I would be grateful if somebody explained me why this doesnt work or would send me a working code snippet for me to use for only one purpose: displaying images.
Once i install the app on my phone and open it I wait exactly 4 seconds (thats how much im delaying it by yhe code above) and it simply crashes
I think that you should be execute the line
iv.setImageResource(R.drawable.loading);
inside UI thead. Look at this answer
Initialise your imageview in onCreate() method not in initializeArks() method and dont declare them as static like as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_game);
iv= (ImageView) findViewById(R.id.imageView); //<==== initialise in onCreate
tw= (TypeWriter) findViewById(R.id.textView);
//initialize arks
new Timer().schedule(new TimerTask() {
#Override
public void run() {
initializeArks();
}
}, 2000);
}
And declared variable as
public ImageView iv;
public TypeWriter tw;
I couldnt find a way to achive what I wanted so I looked in the big picture, and my scenario allowed me to not have an ImageView. Fortunately changing the layout background does the trick for me and it doesnt have any negative effects for my project. Thank you for your wasted time here
It seems like you do not run on the UI Thread.
Try to run inside your thread like this:
runOnUiThread(new Runnable() {
#Override
public void run() {
initializeArks();
}
});

using #background over two activities

Is it possible to create a background thread using andriodannotations thats starts in one activity and finish in another activity.
here is what i thought migh have worked
ActivityA
public static LoadingDialog LoadingScreen = new LoadingDialog();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//.....
LoadingScreen.CreateDialog(context);
}
Background Class
public class LoadingDialog
{
private Dialog loader_dialog;
#Background
public void CreateDialog(Context mContext)
{
loader_dialog = new Dialog(mContext,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
loader_dialog.setContentView(R.layout.loading_screen);
loader_dialog.show();
}
public void Remove()
{
loader_dialog.dismiss();
}
}
the dialog displays correctly but when I finish() activityA to start activityB the thread appears to be killed to and i get a black screen. Any help on this would be much appreciated.

Don't show ads for Android API 8 or lower

Users with Android version 2.2 (API 8) don't seem to appreciate the advertisements that are in my Android application. I would like to disable ads for this Android version or lower. Is there some kind of if/else statement that can prevent the showAdsBanner() and showAdsInterstitial() functions from being loaded? This way the ads won't be loaded and shown to my users.
public class App extends DroidGap implements AdListener
{
private Handler mHandler = new Handler();
private AdView adView;
private InterstitialAd interstitial;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.loadUrl(Config.getStartUrl());
showAdsBanner();
mHandler.postDelayed(new Runnable() {
public void run() {
showAdsInterstitial();
}
}, 2500);
}
}
I'm fairly new to Java and I can't seem to find a working solution. I would greatly appreciate any help. Thank you.
http://developer.android.com/reference/android/os/Build.VERSION.html#SDK_INT
if(Build.VERSION.SDK_INT > 8) {
showAdsBanner();
mHandler.postDelayed(new Runnable() {
public void run() {
showAdsInterstitial();
}
}, 2500);
}

struggling with timer in android

All I am looking to do is run some function 5 seconds after the app starts. However i keep getting a "force close" after 5 sec. Is timerTask even the correct function to be using in a situation like this? How about if i want to press a button later in the program, and have an event occur 5 seconds after user presses the button?
package com.timertest;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class timerTest extends Activity {
Timer timer = new Timer();
TextView test;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
test = (TextView)findViewById(R.id.test);
timer.schedule(task, 5000);
}
TimerTask task = new TimerTask() {
#Override
public void run() {
test.setText("task function run");
}
};
}
A TimerTask will run on a background thread but you're trying to change UI. You want to run your operation on the UI thread instead. Android traditionally uses messages posted to a Handler to do this. A Handler will not create a new thread, when used as shown below it will simply attach to the same message queue that your app uses to process other incoming UI events.
public class Test extends Activity {
private final Handler mHandler = new Handler();
private TextView mTest;
private Runnable mTask = new Runnable() {
public void run() {
mTest.setText("task function run");
}
};
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTest = (TextView) findViewById(R.id.test);
mHandler.postDelayed(mTask, 5000);
}
#Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(mTask);
}
}
If you want to execute some code after 5 secs try the following...
new CountDownTimer(5000,5000)
{
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
// DO YOUR OPERATION
}
}.start();

Categories

Resources