我想更改 Android 上的 GifImageView 位置
I want to change the GifImageView location on Android
package com.bariskarapelit.stajprojesi_1;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.VideoView;
import androidx.appcompat.app.AppCompatActivity;
import android.view.KeyEvent;
import com.twilio.video.CameraCapturer;
import com.twilio.video.LocalVideoTrack;
import com.twilio.video.VideoTextureView;
import pl.droidsonroids.gif.GifImageView;
import android.view.MotionEvent.*;
import static androidx.core.view.ViewCompat.getX;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity
{
ImageButton button,button1,button2;
GridLayout gridLayout;
ImageView circle,dislike,like;
VideoView videoView;
String videoPath;
VideoTextureView videoTextureView;
Uri uri;
GifImageView gifImageView;
MotionEvent event;
private LayoutParams layoutParams;
int windowwidth;
int windowheight;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= findViewById(R.id.circle);
button1=findViewById(R.id.dislike);
button2=findViewById(R.id.like);
circle=findViewById(R.id.imageView);
dislike=findViewById(R.id.imageView2);
like=findViewById(R.id.imageView3);
gifImageView= findViewById(R.id.gift);
circle.setVisibility(View.INVISIBLE);
dislike.setVisibility(View.INVISIBLE);
like.setVisibility(View.INVISIBLE);
//videoView=findViewById(R.id.video_view_top_right);
//Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video);
//videoView.setVideoURI(uri);
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
gifImageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
LayoutParams layoutParams = (LayoutParams) gifImageView.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams.leftMargin = x_cord - 25;
layoutParams.topMargin = y_cord - 75;
gifImageView.setLayoutParams(layoutParams);
break;
default:
break;
}
return true;
}
});
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Toast.makeText(MainActivity.this,"Circle",Toast.LENGTH_LONG).show();
gifImageView.setImageResource(R.drawable.daire);
}
});
button1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Toast.makeText(MainActivity.this,"Dislike",Toast.LENGTH_LONG).show();
gifImageView.setImageResource(R.drawable.dislike);
}
});
button2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Toast.makeText(MainActivity.this,"Like",Toast.LENGTH_LONG).show();
gifImageView.setImageResource(R.drawable.like);
}
});
}
}
我想更改 Android 上的 GifImageView 位置。通过获取用户使用 OnTouchListener 方法进入屏幕的点的位置。我想在 GifImageView 重定位中使用这个位置。你能帮帮我吗?
gifImageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
LayoutParams layoutParams = (LayoutParams) gifImageView.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams.leftMargin = x_cord - 25;
layoutParams.topMargin = y_cord - 75;
gifImageView.setLayoutParams(layoutParams);
break;
default:
break;
}
return true;
}
});
我尝试使用上面的代码更改位置,但包含以下错误:
E/InputEventReceiver: Exception dispatching input event.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
at com.bariskarapelit.stajprojesi_1.MainActivity.onTouch(MainActivity.java:79)
at android.view.View.dispatchTouchEvent(View.java:13469)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:742)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1880)
at android.app.Activity.dispatchTouchEvent(Activity.java:3494)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:700)
at android.view.View.dispatchPointerEvent(View.java:13721)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6175)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5953)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5402)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5455)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5421)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5580)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5429)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5637)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5402)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5455)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5421)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5429)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5402)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8467)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8387)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8340)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8582)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:198)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:181)
at android.app.ActivityThread.main(ActivityThread.java:7094)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Xml代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.twilio.video.VideoTextureView
android:id="@+id/video_view_top_right"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<pl.droidsonroids.gif.GifImageView
android:id="@+id/gift"
android:layout_width="106dp"
android:layout_height="106dp"
android:layout_gravity="center"
android:layout_marginTop="492dp"
android:layout_marginEnd="168dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="70dp"
android:layout_height="58dp"
android:layout_marginStart="61dp"
android:layout_marginTop="244dp"
android:layout_marginEnd="40dp"
app:layout_constraintEnd_toStartOf="@+id/imageView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/launch1" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="70dp"
android:layout_height="58dp"
android:layout_marginTop="244dp"
android:layout_marginEnd="40dp"
app:layout_constraintEnd_toStartOf="@+id/imageView3"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/launch2" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="70dp"
android:layout_height="58dp"
android:layout_marginEnd="60dp"
android:layout_marginBottom="428dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@mipmap/launch3" />
<ImageButton
android:id="@+id/circle"
android:layout_width="51dp"
android:layout_height="51dp"
android:layout_marginStart="60dp"
android:layout_marginBottom="4dp"
android:background="@mipmap/launch1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/dislike"
app:layout_constraintHorizontal_bias="0.574"
app:layout_constraintStart_toStartOf="parent" />
<ImageButton
android:id="@+id/dislike"
android:layout_width="51dp"
android:layout_height="51dp"
android:background="@mipmap/launch2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.499"
app:layout_constraintStart_toStartOf="parent"></ImageButton>
<ImageButton
android:id="@+id/like"
android:layout_width="51dp"
android:layout_height="51dp"
android:layout_marginEnd="60dp"
android:layout_marginBottom="4dp"
android:background="@mipmap/launch3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.447"
app:layout_constraintStart_toEndOf="@+id/dislike"></ImageButton>
</androidx.constraintlayout.widget.ConstraintLayout>
selectedImage = R.drawable.ic_dislike3;
package com.bariskarapelit.stajprojesi_1;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.VideoView;
import androidx.appcompat.app.AppCompatActivity;
import android.view.KeyEvent;
import com.twilio.video.CameraCapturer;
import com.twilio.video.LocalVideoTrack;
import com.twilio.video.VideoTextureView;
import pl.droidsonroids.gif.GifImageView;
import android.view.MotionEvent.*;
import static androidx.core.view.ViewCompat.getX;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity
{
ImageButton button,button1,button2;
GridLayout gridLayout;
ImageView circle,dislike,like;
VideoView videoView;
String videoPath;
VideoTextureView videoTextureView;
Uri uri;
GifImageView gifImageView;
MotionEvent event;
private LayoutParams layoutParams;
int windowwidth;
int windowheight;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= findViewById(R.id.circle);
button1=findViewById(R.id.dislike);
button2=findViewById(R.id.like);
circle=findViewById(R.id.imageView);
dislike=findViewById(R.id.imageView2);
like=findViewById(R.id.imageView3);
gifImageView= findViewById(R.id.gift);
circle.setVisibility(View.INVISIBLE);
dislike.setVisibility(View.INVISIBLE);
like.setVisibility(View.INVISIBLE);
//videoView=findViewById(R.id.video_view_top_right);
//Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video);
//videoView.setVideoURI(uri);
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
gifImageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
LayoutParams layoutParams = (LayoutParams) gifImageView.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams.leftMargin = x_cord - 25;
layoutParams.topMargin = y_cord - 75;
gifImageView.setLayoutParams(layoutParams);
break;
default:
break;
}
return true;
}
});
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Toast.makeText(MainActivity.this,"Circle",Toast.LENGTH_LONG).show();
gifImageView.setImageResource(R.drawable.daire);
}
});
button1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Toast.makeText(MainActivity.this,"Dislike",Toast.LENGTH_LONG).show();
gifImageView.setImageResource(R.drawable.dislike);
}
});
button2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Toast.makeText(MainActivity.this,"Like",Toast.LENGTH_LONG).show();
gifImageView.setImageResource(R.drawable.like);
}
});
}
}
我想更改 Android 上的 GifImageView 位置。通过获取用户使用 OnTouchListener 方法进入屏幕的点的位置。我想在 GifImageView 重定位中使用这个位置。你能帮帮我吗?
gifImageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
LayoutParams layoutParams = (LayoutParams) gifImageView.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams.leftMargin = x_cord - 25;
layoutParams.topMargin = y_cord - 75;
gifImageView.setLayoutParams(layoutParams);
break;
default:
break;
}
return true;
}
});
我尝试使用上面的代码更改位置,但包含以下错误:
E/InputEventReceiver: Exception dispatching input event.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
at com.bariskarapelit.stajprojesi_1.MainActivity.onTouch(MainActivity.java:79)
at android.view.View.dispatchTouchEvent(View.java:13469)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2845)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:742)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1880)
at android.app.Activity.dispatchTouchEvent(Activity.java:3494)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:700)
at android.view.View.dispatchPointerEvent(View.java:13721)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6175)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5953)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5402)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5455)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5421)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5580)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5429)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5637)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5402)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5455)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5421)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5429)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5402)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8467)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8387)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8340)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8582)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:198)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:181)
at android.app.ActivityThread.main(ActivityThread.java:7094)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Xml代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.twilio.video.VideoTextureView
android:id="@+id/video_view_top_right"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<pl.droidsonroids.gif.GifImageView
android:id="@+id/gift"
android:layout_width="106dp"
android:layout_height="106dp"
android:layout_gravity="center"
android:layout_marginTop="492dp"
android:layout_marginEnd="168dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="70dp"
android:layout_height="58dp"
android:layout_marginStart="61dp"
android:layout_marginTop="244dp"
android:layout_marginEnd="40dp"
app:layout_constraintEnd_toStartOf="@+id/imageView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/launch1" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="70dp"
android:layout_height="58dp"
android:layout_marginTop="244dp"
android:layout_marginEnd="40dp"
app:layout_constraintEnd_toStartOf="@+id/imageView3"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/launch2" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="70dp"
android:layout_height="58dp"
android:layout_marginEnd="60dp"
android:layout_marginBottom="428dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@mipmap/launch3" />
<ImageButton
android:id="@+id/circle"
android:layout_width="51dp"
android:layout_height="51dp"
android:layout_marginStart="60dp"
android:layout_marginBottom="4dp"
android:background="@mipmap/launch1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/dislike"
app:layout_constraintHorizontal_bias="0.574"
app:layout_constraintStart_toStartOf="parent" />
<ImageButton
android:id="@+id/dislike"
android:layout_width="51dp"
android:layout_height="51dp"
android:background="@mipmap/launch2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.499"
app:layout_constraintStart_toStartOf="parent"></ImageButton>
<ImageButton
android:id="@+id/like"
android:layout_width="51dp"
android:layout_height="51dp"
android:layout_marginEnd="60dp"
android:layout_marginBottom="4dp"
android:background="@mipmap/launch3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.447"
app:layout_constraintStart_toEndOf="@+id/dislike"></ImageButton>
</androidx.constraintlayout.widget.ConstraintLayout>
selectedImage = R.drawable.ic_dislike3;