-1

Java codes:

package com.bariskarapelit.stajprojesi_1;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.twilio.video.VideoTextureView;

import pl.droidsonroids.gif.GifImageView;

public class MainActivity extends Activity
{

    GridLayout gridLayout;
    VideoView videoView;
    String videoPath;
    VideoTextureView  videoTextureView;
    Uri uri;
    GifImageView gifImageView;


    int windowwidth;
    int windowheight;

    private LinearLayout.LayoutParams layoutParams;  //linear layout yaptığın zaman buraya dikkat et

    Handler handler;



    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        gifImageView= findViewById(R.id.gift);

        gifImageView.setVisibility(View.INVISIBLE);

        FrameLayout frameLayout = findViewById(R.id.frame_layout);






        //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();

        handler = new Handler();

        frameLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {


                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) frameLayout.getLayoutParams();
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:




                        break;
                    case MotionEvent.ACTION_MOVE:



                        System.out.println("TIKLANDI");


                        gifImageView.setVisibility(View.VISIBLE);


                        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 - 53;
                        layoutParams.topMargin = y_cord - 53;

                        gifImageView.setLayoutParams(layoutParams);

                        break;
                    default:
                        break;
                }


                return true;
            }
        });



    }
}

Xml Codes:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout


 

       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">
    
       <FrameLayout
           android:id="@+id/frame_layout"
           android:layout_width="match_parent"
           android:layout_height="match_parent">
    
    
           <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:background="@drawable/like_gif"
               />
    
       </FrameLayout>
    
    
    
    
    </LinearLayout>

Error Code:

    2020-08-13 23:09:06.762 12686-12686/com.bariskarapelit.stajprojesi_1 E/AndroidRuntime: FATAL 
    EXCEPTION: main
    Process: com.bariskarapelit.stajprojesi_1, PID: 12686
    java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to 
    android.widget.FrameLayout$LayoutParams

FrameLayout's setOnTouchListener method failed. With this method, I wanted to show imageview where you touched in FrameLayout, but it gave the error "android.widget.LinearLayout $ LayoutParams cannot be cast to android.widget.FrameLayout $ LayoutParams". Can you help me?

Ryan M
  • 18,333
  • 31
  • 67
  • 74
BARIŞ
  • 21
  • 7

1 Answers1

0

1- First of all if you want to change "gifImageView" element's layout params you should take its layout params.

2- As your gifImageView is inside FrameLayout you should use Frame Layout params rather than Lineer Layout Params

So changing OnTouch Method's related part with this should help

FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams)
    gifImageView.getLayoutParams();

3- if you want to move your whole frameLayout move with the touch you should change the related part with this:

frameLayout.setLayoutParams(layoutParams);
Eren Tüfekçi
  • 2,463
  • 3
  • 16
  • 35